|--List: The elements are ordered (get out as they are stored, and the order will not be messed up). The elements can be repeated (there is a 3 on the corner mark 1, and there is a 3 on the corner mark 2) because of this set The system has indexes,
|-- ArrayList: The underlying data structure uses an array structure (the length of the array is 50% longer than the variable) (the feature is that the query is very fast, but the addition and deletion are slow) threads are not synchronized
|-- LinkedList: The underlying data structure is a linked list structure (the characteristics are slower query and faster addition and deletion)
|-- Vector: The underlying layer is the array data structure thread synchronization (the array length is 100% extended by variable) (both queries and additions and deletions are very slow, and it was replaced by ArrayList)
List: A unique method. Any method that can operate angle marks is a unique method of this system.
increase
The code copy is as follows:
boolean add(int index, E element)
boolean addAll(index,Collection)
The code copy is as follows:
public static void List_add(){
ArrayList a1 = new ArrayList();
a1.add("java");
a1.add("php");//Elements in the List collection can be repeated
a1.add(".net");
System.out.println("original collection:"+a1);
a1.add(1, "Flash");
a1.add(0, "ps");
System.out.println(a1);
ArrayList a2 = new ArrayList();
a2.add("javascript");
a2.add("3dMax");
a2.add("IBM");
a1.addAll(0, a2);
System.out.println(a1);
}
Delete elements at the specified location
The code copy is as follows:
boolean remove(int index)
The code copy is as follows:
public static void List_remove(){
ArrayList a1 = new ArrayList();
a1.add("javascript");
a1.add("php");
a1.add("flash");
System.out.println("original collection:"+a1);
a1.remove(0);
System.out.println(a1);
}
Modify the element of the specified angle set(int index, E element) The returned element is modified
The code copy is as follows:
public static void List_set() {
ArrayList a1 = new ArrayList();
a1.add("javascript");
a1.add("php");
a1.add(".net");
System.out.println("original collection:"+a1);
a1.set(1, "falsh");
System.out.println(a1);
}
check
The code copy is as follows:
get(int index) Returns the element at the specified position in the list
subList(int fromIndex, int toIndex) Returns some elements between fromIndex (including) and toIndex (excluding) specified in the list.
The code copy is as follows:
public static void List_get() {
ArrayList a1 = new ArrayList();
a1.add("java");
a1.add("php");
a1.add("flash");
System.out.println(a1.get(0));//Get the element of the specified angle point. With this method, you can traverse all elements in the set.
System.out.println(a1.subList(1, 3));//Get the element in a certain part of the collection, including the head but not the tail
}
List collection-specific iterator: ListIterator (is a subinterface of Iterator)
Notice:
During iteration, elements in the collection cannot be operated through the method of the collection object
Because a ConcurrentModificationException exception (concurrent exception) will occur
Therefore, when iterating, you can only use the iterator method to create elements
Because the Iterator method is limited, it can only judge, remove, and delete elements.
If you want other operations such as adding, modifying, etc., you need to use its sub-interface, ListIterator
This interface can only be obtained through the listIterator method of the List collection
The code copy is as follows:
public class ListIteratorDemo {
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
a1.add("java01");
a1.add("java02");
a1.add("java03");
a1.add("java04");
System.out.println("Original collection is: "+a1);
/*Preparing to add or delete elements during the iteration process
Iterator it = al.iterator();
while (it.hasNext()){
Object obj = it.next();
if (obj.equals("java02"))
//al.add("java008");// Concurrent exception will occur because the iterator is operating the collection and can no longer use the collection method to operate the collection.
it.remove();//Remove the java02 reference from the collection
System.out.println("obj:"+obj);
}
*/
//Only ListIterator of List has the functions of adding, deleting, modifying, and checking, because only List has the index
ListIterator li = a1.listIterator();
while (li.hasNext()){
if(li.next().equals("java02"))
//li.add("java009");
li.set("java006");
}
}
}
Vector: Enumeration is a unique way of fetching in Vector, which is very similar to an iterator (in fact, enumeration and iteration are the same) and have been replaced by an iterator.
The code copy is as follows:
public class VectorDemo {
public static void main(String[] args) {
Vector v = new Vector();
v.add("java01");
v.add("java02");
v.add("java03");
v.add("java04");
for(Enumeration en = v.elements();en.hasMoreElements();){
System.out.println(en.nextElement());
}
}
}
LinkedList:
Special method:
addFirst(); add element at the head addLast(); add element at the tail
getFirst(); getLast(); getget element but not delete element. If there are no elements in the collection, NoSuchElementException will appear
removeFirst(); removeLast(); gets the element but removes the element. If there are no elements in the collection, NoSuchElementException will appear
Alternative approaches appear in JDK1.6
offerFirst(); offerLast();
peekFirst(); peekLast(); Gets the element, but the element is not deleted. If there are no elements in the collection, null will be returned
pollFirst(); pollLast(); Gets the element, but the element is deleted. If there are no elements in the collection, null will be returned
The code copy is as follows:
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList link = new LinkedList();
link.add("java01");
link.add("java02");
link.add("java03");
link.add("java04");
while(!link.isEmpty()){
System.out.println((link.removeLast()));
}
}
}