In the Collection system, the List collection is ordered, and the insertion position of each element can be precisely controlled. Elements can be accessed and traversed through indexes. List collection refers to a collection composed of List interface and all implementation classes of List interface. List ordered collection is also called sequence. Users of this interface can precisely control the insertion position of each element in the list, and users can access it through integer index element, and searches for an element in the list. Unlike Sets, Lists generally allow duplicate elements.
1) Ordered : The order of stored and retrieved elements is consistent;
2) Repeatable : The stored elements can be repeated.
boolean add(E e) : Add an element to the collection.
void add(int index, E element) : Add an element at the specified position.
boolean addAll(Collection<? extends E> c) : Adds an element of a collection to the collection.
void clear() : Delete all elements in the collection.
E remove(int index) : Remove elements according to the specified index and return the deleted elements.
boolean remove(Object o) : Removes the specified element from the collection.
boolean removeAll(Collection<?> c) : Removes a specified collection element from the collection.
E set(int index, E element) : Modify the element at the specified index position to the specified value and return the value before modification.
E get(int index) : Get the element at the specified position.
Iterator iterator() : is used to obtain each element in the collection.
boolean isEmpty() : Determines whether the collection is empty.
boolean contains(Object o) : Determine whether the specified element exists in the collection.
boolean containsAll(Collection<?> c) : Determine whether the element in the specified collection exists in the collection.
int size() : Get the number of elements in the collection.
Object[] toArray() : Turn the collection into an array.
Because it is obtained through the listIterator() method of the List collection, it is called an iterator unique to the List collection.
Commonly used methods of ListIterator are as follows:
1) E next() : Returns the next element in the iterator.
2) boolean hasNext() : Returns true if the iteration has more elements.
3) E previous() : Returns the previous element in the list.
4) boolean hasPrevious() : Returns true if this list iterator has more elements when traversing the list in the opposite direction.
5) void add(E e) : Insert the specified element into the list.
For example:
importjava.util.ArrayList;importjava.util.List;importjava.util.ListIterator;publicclassMain{publicstaticvoidmain(String[]args){List<String>l=newArrayList<String>();l.add(hello);l. add(world);l.add(java);ListIterator<String>listit=l.listIterator();while(listit.hasNext()){Strings=listit.next();System.out.println(s); }System.out.println(www.dotcpp.com);while(listit.hasPrevious()){Stringsp=listit.previous();System.out.println(sp);}System.out.println(www.dotcpp .com);ListIterator<String>listit2=l.listIterator();while(listit2.hasNext()){Strings2=listit2.next();if(s2.equals(world)){listit2.add(dotcpp); }}System.out.println(l);}}
The running results are as follows:
helloworldjavawww.dotcpp.comjavaworldhellowww.dotcpp.com[hello,world,dotcpp,java]