List Interface:
1. The list interface is a sub-interface of the Collection, and the elements in the container class implementing the list interface are sequential and repeatable;
2. The elements in the list container correspond to the ordinal of an integer to record its position in the container, and the elements in the container can be accessed according to the sequence number;
3. The List container class provided by J2SDK has ArrayList, LinkedList and so on.
4. List interface Common methods:
4.1 Object get(int index): Returns the element at the specified position in the list;
4.2 Set(int index, Object Element): Replaces the element in the specified position in the list with the specified element (optional operation);
4.3 void add(int index, Object Element): Inserts the specified element at the specified position in the list (optional operation);
4.4 Boolean Remove(Object o): Removes the first occurrence of the specified element (if present) from this list (optional operation);
4.5 object[] toArray(): Returns an array that contains all the elements in the list in the appropriate order (from the first element to the last element);
4.6 <T> t[] toArray(t[] a): Returns an array of all the elements in the list, in the appropriate order (from the first element to the last element);
Assume that x is a well-known list that contains only strings. The following code is used to dump the list to a newly allocated String array:
string[] y = X.toarray (new string[0]);
Note thatToArray (new object[0]) and ToArray () are functionally identical;
4.7 int indexOf(Object O): Returns the index of the specified element that first appears in this list, or 1 if the list does not contain the element;
4.8 int lastIndexOf(Object O): Returns the index of the last occurrence of the specified element in this list, or 1 if the list does not contain this element;
Demo_1:
Import Java.util.*;class Test {public static void main (string[] args) {linkedlist L1 = new linkedlist<> (); for (int i= 0;i<=5;i++) {L1.add ("a" +i);} System.out.println (L1); Output: [A0, a1, A2, A3, A4, A5]l1.add (3, "A100"); System.out.println (L1); Output: [A0, a1, A2, A100, A3, A4, A5]l1.set (6, "a200"); System.out.println (L1); Output: [A0, a1, A2, A100, A3, A4, A200]system.out.print ((String) L1.get (2) + ""); Output: A2System.out.print (L1.indexof ("A3") + ""); Output: A2 4system.out.println (L1.indexof ("12")); Output: A2 4-1l1.remove (1); System.out.println (L1); Output: [A0, a2, A100, A3, A4, A200]}
List interface of the Java container