SoFunction
Updated on 2025-03-06

Detailed explanation of Java List usage examples

Detailed explanation of Java List usage examples

The principle of variable arrays in Java is to constantly create new arrays and add the original array to the new array. The following is a detailed explanation of the usage of Java List.

  • List: Elements are ordered (get out as they are stored, and the order will not be messed up). Elements can be repeated (there is a 3 on corner mark 1, and there is a 3 on corner mark 2) because the collection system has an index.
  • 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% longer than variable) (both query or addition and deletion 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.

boolean add(int index, E element)
boolean addAll(index,Collection)
public static void List_add(){
   ArrayList a1 = new ArrayList();
   ("java");
   ("php");//Elements in List collection can be repeated  (".net");
   ("Original Collection:"+a1);
   (1, "Flash");
   (0, "ps");  
   (a1);

  ArrayList a2 = new ArrayList();
  ("javascript");
  ("3dMax");
  ("IBM");

  (0, a2);
  (a1);
}

Delete elements at the specified location

 boolean remove(int index)
public static void List_remove(){
   ArrayList a1 = new ArrayList();
   ("javascript");
   ("php");
   ("flash");
   ("Original Collection:"+a1);

   (0);
   (a1);
}

Modify the element of the specified angle set(int index, E element) The returned element is modified

public static void List_set() {
   ArrayList a1 = new ArrayList();
   ("javascript");
   ("php");
   (".net");
   ("Original Collection:"+a1);

   (1, "falsh");
   (a1);
}

check

get(int index) // Returns the element at the specified position in the listsubList(int fromIndex, int toIndex)  // Returns some elements between fromIndex (including ) and toIndex (excluding) specified in the list.
public static void List_get() {
   ArrayList a1 = new ArrayList();
   ("java");
   ("php");
   ("flash");

   ((0));//Get the element of the specified angle point. With this method, you can traverse all elements in the collection
   ((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 ConcurrentModificationException exception (concurrent exception) will occur, so when iterator, you can only use the iterator method to create elements. Because the Iterator method is limited, you can only judge, remove, and delete the 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.

public class ListIteratorDemo {
   public static void main(String[] args) {
     ArrayList a1 = new ArrayList();
     ("java01");
     ("java02");
     ("java03");
     ("java04");

     ("The original collection is:"+a1);

   /*Preparing to add or delete elements during the iteration process
    Iterator it = ();
    While (()){
    Object obj = ();

    if (("java02"))
    //("java008");// Concurrent exception will occur because the iterator is operating the collection and can no longer use the collection method to operate the collection.
    ();//Remove the reference of java02 from the collection
    ("obj:"+obj);
     }
     */  
  //Only ListIterator of List has the functions of adding, deleting, modifying, and checking, because only List has the index    ListIterator li = ();
     while (()){
     if(().equals("java02"))
     //("java009");
     ("java006");
    }  
  }
}

Vector: Enumeration is a unique way of fetching in Vector, which is very similar to iterators (in fact, enumeration and iterative are the same) and have been replaced by iterators.

 public class VectorDemo {
   public static void main(String[] args) {
     Vector v = new Vector();
     ("java01");
     ("java02");
     ("java03");
     ("java04");

     for(Enumeration en = ();();){
      (());
    }
  }
}

LinkedList:

Special method:

addFirst(); add element at the head

addLast(); add element at the end

getFirst(); getLast();

Gets elements but does not delete them. If there are no elements in the collection, NoSuchElementException will appear

removeFirst();   removeLast();

Get the element but delete 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

 public class LinkedListDemo {
   public static void main(String[] args) {
     LinkedList link = new LinkedList();
     ("java01");
     ("java02");
     ("java03");
     ("java04");

     while(!()){
      ((()));
    }
  }
}

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!