The List interface

List interface: Stores ordered, repeatable data

Three implementation classes:

  • ArrayList
  • LinkedList
  • Vector

Similarities and differences of the three:

  • Same: The three classes all implement the interface of List and store data with the same characteristics, ordered and repeatable
  • Vision:
    • ArrayList: As the main implementation class of the List interface, it is not thread safe and efficient. The bottom layer uses Object [] for storage
    • LinkedList: the underlying storage uses a two-way LinkedList, which is more efficient for frequent insert and delete operations
    • Vector: Ancient implementation class, thread-safe, inefficient. The bottom layer uses Object [] for storage

Common methods in the List interface:

  • Add: add
  • Delete: remove (int index) /remove (Object obj)
  • Change: the set
  • Check: the get
  • Length: the size
  • Through:
    • The Iterator Iterator
    • Enhanced for loop
    • Normal for loop

ArrayList

Additional methods

  • List.add (1, “BB”) to add elements to the specified index
  • AddALL adds all elements in another List
  • List.get (0) gets the specified subscript element
  • List.indexof (456), returns the indexOf the element if it exists, or -1 if it does not
  • List.remove (2), removes the element at the specified index position and returns the element
  • List.set (1, “CC”) sets the element at the specified index position
  • List. subList(2, 4), returns the list of the specified range
list.add(1, "BB"); System.out.println(list); System.out.println("**********"); //addALL List list1 = Arrays.asList(1, 2, 3); list.addAll(list1); System.out.println(list1); System.out.println(list.size()); System.out.println("**********"); Println (list.get(0)); system.out.println (list.get(0)); System.out.println(list.get(3)); //System.out.println(list.get(45)); System.out.println("**********"); // Return -1 int index = list.indexof (456); // Return -1 int index = list.indexof (456); System.out.println(index); index = list.indexOf(46546); System.out.println(index); System.out.println(list.lastIndexof (456))); System.out.println("**********"); Object obj = list.remove(2); // Remove the element at the specified index position and return this element. System.out.println(obj); System.out.println(list); System.out.println("**********"); // set list. Set (1, "CC"); System.out.println(list); System.out.println("**********"); List sublist = list.sublist (2, 4); System.out.println(sublist); System.out.println(list);Copy the code