List of Java collection framework

1、 List:

1. Unique common methods: (one common feature is that corner markers can be operated)

(1) , add

Void add (int index, e element): inserts an element at the specified position in the list

Void addall (int index, collection e): inserts all elements in the specified collection into the specified location in the list

(2) , delete

E remove (int index): deletes the element at the specified position and returns the element;

(3) . modification

E set (int index, e element): replaces the element at the specified position and returns the replaced element

(4) , acquisition

Int indexof (object o): returns the index of the first occurrence of the specified element. If the list does not contain, it returns - 1;

E get (int index): returns the element at the specified position;

List < E > sublist (int fromindex, int toindex): returns the partial view (list) between fromindex (including) and toindex (excluding) specified in the list;

2. When iterating over a list, you cannot operate on the list (add, delete, or modify)

You can use listiterator, a sub interface of the iterator interface, to perform operations on the list during the iteration process The listiterator() method returns the list iterator of this list element. This iterator can iterate in reverse order (hasprevious() method and previous()). Note that the listiterator object should be operated when operating on the list, for example:

While(it.hasNext()){

//Note that you can't use list here add(“demo”)!!!!!!

It. add(“demo”);

}

3. Common objects

|--Vector: the internal array data structure is synchronous. The array is 100% extended, and the addition and deletion queries are very slow.

|--ArrayList: the internal array data structure is asynchronous. It replaces vector and extends the array by 50%. The number of queries is fast.

|--LinkedList: the internal linked list data structure is asynchronous, and the speed of adding and deleting elements is fast.

4. Detailed explanation of common objects

1、 Vector

//Enumeration

Enumeration en = v.elements();

While(en.hasMoreElements()){

System. out. println(en.nextElement());

}

Enumeration is the same as iterator, but there is no remove method. Iterator is recommended

2、 LinkedList

Linkedlist. add(“demo1”);

Linkedlist. add(“demo2”);

Linkedlist. add(“demo3”);

System. out. print(linkedlist);

Output [demo3, demo2, Demo1];

When iterating with LinkedList, the elements are output in reverse order

Getfirst() of LinkedList throws an exception NoSuchElementException when no data is obtained, but peekfirst() in version 1.6 will return null, (pollfirst() corresponds to removefirst())

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>