Home  >  Article  >  Java  >  Detailed explanation of examples of List usage in Java

Detailed explanation of examples of List usage in Java

黄舟
黄舟Original
2017-09-07 10:17:021242browse

This article mainly introduces relevant information about Java List usage examples. Friends who need it can refer to

Java List usage examples

Can be found in Java The principle of variable array is to continuously create new arrays and add the original array to the new array. The usage of Java List is explained in detail below.

  • List: The elements are in order (you can take them out how you save them, the order will not be messed up), and the elements can be repeated (there is a 3 on subscript 1, and a 3 on subscript 2) There can also be 3) Because the collection system has an index

  • ArrayList: the underlying data structure uses an array structure (the array length is variable and can be extended by 50%) ( The characteristic is that the query is fast, but the addition and deletion are slow) the thread is not synchronized

  • LinkedList: The underlying data structure is a linked list structure (the characteristic is that the query is slow and the addition and deletion are fast)

  • Vector: The bottom layer is the array data structure thread synchronization (the array length is variable and 100% extended) (no matter query or addition or deletion is very slow, replaced by ArrayList)

List: Unique methods. All methods that can operate corner markers are unique to the system.


boolean add(int index, E element)
boolean addAll(index,Collection)


public static void List_add(){
   ArrayList a1 = new ArrayList();
   a1.add("java");
   a1.add("php");//List集合中的元素可以重复
  a1.add(".net");
   System.out.println("原集合:"+a1);
   a1.add(1, "Flash");
   a1.add(0, "ps");  
   System.out.println(a1);

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

  a1.addAll(0, a2);
  System.out.println(a1);
}

Delete the element at the specified position


 boolean remove(int index)


public static void List_remove(){
   ArrayList a1 = new ArrayList();
   a1.add("javascript");
   a1.add("php");
   a1.add("flash");
   System.out.println("原集合:"+a1);

   a1.remove(0);
   System.out.println(a1);
}

Modify the element at the specified index set(int index, E element) Returns the modified That element


public static void List_set() {
   ArrayList a1 = new ArrayList();
   a1.add("javascript");
   a1.add("php");
   a1.add(".net");
   System.out.println("原集合:"+a1);

   a1.set(1, "falsh");
   System.out.println(a1);
}

Check


get(int index) // 返回列表中指定位置的元素
subList(int fromIndex, int toIndex)  // 返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分元素。


public static void List_get() {
   ArrayList a1 = new ArrayList();
   a1.add("java");
   a1.add("php");
   a1.add("flash");

   System.out.println(a1.get(0));//获取指定角标的元素,有了该方法就可以遍历该集合中的所有元素

   System.out.println(a1.subList(1, 3));//获取集合中某一部分的元素,包含头不包含尾
}

List collection-specific iteration Device: ListIterator (is a sub-interface of Iterator)

Note:

During iteration, you cannot operate the elements in the collection through the methods of the collection object because it will happen ConcurrentModificationException (concurrency exception) Therefore, in the iterator, you can only use the iterator method to create elements. Because the Iterator method is limited, you can only judge, remove, and delete elements. If you want other operations such as adding, To modify, 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();
     a1.add("java01");
     a1.add("java02");
     a1.add("java03");
     a1.add("java04");

     System.out.println("原集合是:"+a1);

   /*在迭代过程中准备添加或者删除元素
   Iterator it = al.iterator();
   while (it.hasNext()){
     Object obj = it.next();

     if (obj.equals("java02"))
     //al.add("java008");//会出现并发异常,因为迭代器正在操作集合,不能再用集合的方法操作集合了
     it.remove();//将java02的引用从集合中删除了
     System.out.println("obj:"+obj);
    }
    */  
  //只有List的listIterator有增,删,改,查这些功能,因为只有List有索引
    ListIterator li = a1.listIterator();
     while (li.hasNext()){
     if(li.next().equals("java02"))
     //li.add("java009");
     li.set("java006");
    }  
  }
}

Vector: Enumeration is the unique way to extract Vector, and Iterators are very similar (in fact, enumerations and iterations are the same) and have been replaced by iterators


##

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

     for(Enumeration en = v.elements();en.hasMoreElements();){
      System.out.println(en.nextElement());
    }
  }
}

LinkedList:

Special methods:

addFirst(); Add an element to the head

addLast(); Add an element to the tail

getFirst(); getLast();

Get elements but do not delete elements. If there are no elements in the collection, NoSuchElementException will occur

removeFirst(); removeLast();

Get the element but delete the element. If there are no elements in the collection, NoSuchElementException will occur

In JDK1.6, alternative methods

offerFirst(); offerLast();

peekFirst(); peekLast() ; Get the element, but the element is not deleted. If there are no elements in the collection, null will be returned

pollFirst(); pollLast(); The element is obtained, but the element is deleted. If there are no elements in the collection, null


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

     while(!link.isEmpty()){
      System.out.println((link.removeLast()));
    }
  }
}
will be returned

The above is the detailed content of Detailed explanation of examples of List usage in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn