In Java, we have seen that although every reference type is compatible with Object, the eight basic types cannot. Therefore, a wrapper class is provided for each of these 8 basic types in Java.
The table can be realized by using an array. Although the array is created with a fixed capacity, you can create a different array with twice the capacity when needed.
int[] arr = new arr[10];
...
int[] newArr = new int[arr.length * 2];
for(int i = 0; i < arr.length; i++)
newArr[i] = arr[i];
arr = newArr;
The implementation of array allows access to the kth element to be executed in linear time, and the entire array is output in linear time. However, it takes O(N) time on average to insert or delete elements, such as deleting the first element in an array, and other elements of the table need to move one position forward.

The linked list consists of a series of nodes, and these nodes do not have to be continuous in memory. Each node contains a table element and a link to the node containing the subsequent element of the element. We call it the next chain. The next chain reference of the last unit is NULL.
In order to execute printList or find(x), we need to traverse the linked list from the head node, and the operation time is linear.
The remove method can be implemented by modifying a next reference.

insert method needs to use the new operator to get a new node.

We have seen that inserting or deleting an item in the linked list does not need to move many items, just design a constant number of node chain changes.
Double-linked list:

public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object var1);
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] var1);
default <T> T[] toArray(IntFunction<T[]> generator) {
return this.toArray((Object[])generator.apply(0));
}
boolean add(E var1);
boolean remove(Object var1);
boolean containsAll(Collection<?> var1);
boolean addAll(Collection<? extends E> var1);
boolean removeAll(Collection<?> var1);
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
Iterator each = this.iterator();
while(each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}
boolean retainAll(Collection<?> var1);
void clear();
boolean equals(Object var1);
int hashCode();
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, 0);
}
default Stream<E> stream() {
return StreamSupport.stream(this.spliterator(), false);
}
default Stream<E> parallelStream() {
return StreamSupport.stream(this.spliterator(), true);
}
}
The Collection interface extends the Iterable interface. Those classes that implement the Iterable interface can have an enhanced for loop that is applied to these classes to observe all of their items.
public static <T> void print(Collection<T> coll){
for(T item : coll)
System.out.println(item);
}
public interface Iterator<E> {
boolean hasNext();
E next();
default void remove() {
throw new UnsupportedOperationException("remove");
}
default void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while(this.hasNext()) {
action.accept(this.next());
}
}
}
The Iterator interface has a remove method, which can delete the latest item returned by next. Using the remove method of Iterator has the following advantages:
public interface List<E> extends Collection<E> {
T get(int index);
T set(int index, T newval);
void add(int index,T x);
void remove(int index);
ListIterator<T> listIterator(int pos);
}
}
List can be implemented in two ways. ArrayList uses a growth array to implement List, and the disadvantages are the same as arrays. The LinkedList uses a double-linked list to achieve. In order to see the difference, we operate certain methods in the List.
public static void makeList1(List<Integer> lst, int N)
{
lst.clear();
for(int i=0; i < N; i++)
lst.add(i);
}
Regardless of whether ArrayList or LinkedList is used, the running time of makeList1 is O(N), because the operation from the end of the table takes a constant time.
If you add some items from the front of the table to construct a List:
public static void makeList2(List<Integer> lst, int N)
{
lst.clear();
for(int i=0; i < N; i++)
lst.add(0,i);
}
Then for LinkedList its running time is O(N), and for ArrayList its running time is O(N2)。
The next program is to calculate the sum of the numbers in List:
public static int sum(List<Integer> lst){
int total = 0;
for( int i = 0; i < N; i++ )
total += lst.get(i);
}
The running time of ArrayList is O(N), but the running time of LinkedList is O(N2), because for LinkedList, the call time to get is O(N) operation. If an enhanced for loop is used, its operation time for any List is O(N).
As an example, we write a program to delete all items with even values in a table.
// Case 1: Delete even values in the table
public static void removeEvensVerl( List<Integer> lst){
int i=0;
while(i<lst.size())
if(lst.get(i)%2==0)
lst.remove(i);
else
i++;
}
But this program is not efficient. We know that for an ArrayList, the use of remove is not efficient; for a LinkedList, the first use of the get method is not efficient, and the second use of the remove method is also inefficient, because of the cost of reaching position i Is expensive.
//Case Two:
public static void removeEvensVer2( List<Integer> lst){
for(Integer it : lst){
if( it%2 == 0)
lst.remove(it);
}
}
We will use the enhanced for loop to traverse the table step by step, which is highly efficient. But using the remove method in Collection to delete an item has the following disadvantages:
//Case Three:
public static void removeEvensVer3( List<Integer> lst){
Iterator<Integer> itr = lst.iterator();
while(itr.hasNext())
if(itr.next() % 2 == 0 )
itr.remove();
}
For a LinkedList, the call to the iterator's remove method only takes a constant time, because the iterator is located near the node that needs to be deleted, so the call to the iterator's remove method only takes a constant time. For ArrayList, even if the iterative is on the node that needs to be deleted, the remove method is still expensive because the array elements need to be moved.
ListIterator extends the function of Iterator in List. The methods previous and hasPrevious make it convenient to move from the back of the table to the front. The add method puts a new item into the table at its current position, and the set method changes the last value seen by the iterator.
public interface ListIterator<E> extends Iterator<E> {
boolean hasPrevious();
E previous();
void set(E e);
void add(E e);
}

Chapter 5 Stacks and Queues 5.1 Overview Stacks and queues are mainly used in the calculation processSave temporary data。 5.1.1 Stack, queue and data usage order Stacks and queues are also the simples...
For the queue, he is as important as the stack, and often the queue and the stack are used together, because the stack is last in, first out, and the queue is first in, first out, which leads to many ...
The priority queue is a data structure that allows at least the insertion and deletion of the two operations. Among them, for the implementation of the priority queue, the binary heap is very common. ...
Ideas: When performing enqueue operations Check if stack_2 is empty If stack_2 is not empty, put the data in stack_2 into stack_1 Push the data that needs to be queued to stack_1 When performing deque...
Consider the following scenario: If there are multiple single-page jobs and a 100-page job waiting to be printed when the printer is free, it may be more reasonable to process the long job last, even ...
1. Table The table referred to here is a general table in the form of A0, A1...An. The special table with size 0 is called an empty table. Any table except the empty table, we say AiSuccessor Ai-1Ai-1...
Abstract data type Abstract data type (ABSTRACT DATA TYPE, ADT) is a collection of some objects with a set of operations Table ADT Shaped like A0,A1,A2,...,AN−1 Table. The size of the table is n...
February 28, 2020 1. What is a stack? A stack is a simple data structure (similar to a linked list) used to store data. The order of the data stack is the key to the stack. Think of a stack of plates ...
Ideas: Stack operation: Add directly to queue_1 Pop operation: If the length of queue_1 is> 1, perform a remove operation on queue_1 and put the data in queue_2 until there is only one element left...
Data structure and algorithm JS description stack and queue Reference Data structure and algorithm JS description, [America] Michael McMillan, translated by Wang Qunfeng and Du Huan Write in front The...