Collection system:
--------------| Collection The root interface of a single-column collection
----------| List If a collection class that implements the List interface, the class has The characteristics are: orderly, repeatable
----------| Set If the set class of Set is implemented, the characteristics of this class are: unordered, non-repeatable
List's unique method: add add(int index, E element) Add an element to the specified index position addAll(int index, Collection<? extends E> c) Add all elements in one collection to another collection according to the index remove remove(int index) Remove according to the index View the specified element in the collection get(int index) Get the element at the specified index in the collection subList(int fromIndex, int toIndex) Specify the start and end positions to get some elements in the collection (the header does not include the end) indexOf(Object o) Get the position (index value) of the first occurrence of the specified element in the collection lastIndexOf(Object o) Get the position (index value) of the last occurrence of the specified element in the collection Modify set(int index, E element) Replace the element in the specified element ( element: the modified value)
Iterator
listIterator() Returns the list iterator of the elements in the list
listIterator(int index) Returns the list iterator of the specified element in the list (index: starting position) Summary: We found that the specific methods in the List interface all have index values, so we If you want a method unique to listIterator: hasPrevious() to determine whether there is a previous element previous() The pointer moves down one bit first, and then takes out the element pointed to by the current pointer next() First takes out the element pointed to by the current pointer, and then moves the pointer down One note: when we start iterating, the pointer defaults to the first element
Let's take a piece of code:
public static void main(String[] args) { List list = new ArrayList(); list.add( "Zhang San" ); list.add( "Li Si" ); list.add( "Wang Wu" ); ListIterator listit = list.listIterator (); while (listit.hasNext ()) { System.out.print(listit.next()+","); } System.out.println(""); while(listit.hasPrevious()) { System.out.print(listit.previous()+","); } }
At this time, are we wondering: why does the second loop output Wang Wu? According to the above statement, can the second cycle only take out Li Si and Zhang San?
First let's look at the definition of next:
next() first takes out the element pointed to by the current pointer, and then moves the pointer down one bit
We can find that: when next removes the last element, the pointer moves down one bit, and then it is judged that there are no elements.
According to the above example, the pointer at this time points to 3 (index)
Let's look at the definition of the previous method again:
previous() The pointer moves down one place first, and then removes the element pointed to by the current pointer
We can find that if the pointer points to 3 at this time, then the pointer moves up one more bit, and the pointer points to 2 at this time, so Wang Wu is output.