Features of List Collection
Ordered: the order of elements stored and retrieved is consistent
There are indexes: elements can be manipulated through indexes
Repeatable: The stored elements can be repeated
List collection method
The methods of Collection are all inherited, and you can use the methods in Collection
In addition, because List collections have indexes, there are many more methods for indexing operations.
Method name | illustrate |
void add (int index,E element) | Insert the specified element at the specified location in this collection |
E remove (int index) | Delete the element at the specified index and return the deleted element |
E set (int index,E element) | Modify the element at the specified index and return the modified element |
E get (int index) | Returns the element at the specified index |
Notice:
There is overloading in the above methods in the List collection. One is to inherit the parameters that need to be provided by the Collection are the given specific objects, and the other is that the parameters that List itself need to be provided are indexes, so the following problems arise.
For example: remove(1), is it necessary to delete whether the generic is an element 1 in the <Integer> collection, or to delete the element on the 1 index
Answer: Priority is given to calling methods with the same type of formal parameters and actual parameters.
Code demo:
import ; import ; public class Test5 { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); (1); (2); (3); (list);//[1, 2, 3] //1 is int type, so the call formal parameter is int type that deletes the element at the specified index. (1); (list);//[1, 3] //Change 1 to Integer type Integer i = (1); //1 in the i variable is the reference data type, so the call formal parameter is Object type to delete the specified element. (i); (list);//[3] } }
How to traverse List collections
In addition to the same iterator traversal, enhanced for traversal, lambda traversal, there are also ordinary for traversal, and list iterator traversal.
Normal for traversal
Code Demo
public class Test6 { public static void main(String[] args) { //Ordinary for traversal List<String> list = new ArrayList<>(); ("aaa"); ("bbb"); ("ccc"); for (int i = 0; i < (); i++) { String str = (i); (str); } //aaa //bbb //ccc } }
List iterator traversal
illustrate
ListIterator inherits from iterator Iterator.
Get list iterator
Method name | illustrate |
ListIterator<E> listIterator () | Returns the 0 index of the iterator object that points to the current collection by default |
Common methods
Method name | illustrate |
boolean hasNext () |
Forward traversal list Determine whether there are elements in the current position. Return true if there is an element, return false if there is no element. |
E next () | Get the element at the current location And move the iterator object to the next position. |
boolean hasPrevious () |
Reverse traversal list Determine whether there are elements in the current position. Return true if there is an element, return false if there is no element (understand) |
E previous () | Get the element at the current location And move the iterator object to the previous position. (learn) |
vold add (E e) | Insert the specified element into the list |
void remove () | Delete the obtained element |
Code Demo
import ; import ; import ; public class Test6 { public static void main(String[] args) { List&lt;String&gt; list = new ArrayList&lt;&gt;(); ("aaa"); ("bbb"); ("ccc"); //Travel printing ListIterator&lt;String&gt; lit1 = (); while (()) { String str = (); (str); } //aaa //bbb //ccc //Traveling addition: add ddd behind aaaa ListIterator&lt;String&gt; lit2 = (); while (()) { String s = (); if (("aaa")) { ("ddd"); } } (list);//[aaa, ddd, bbb, ccc] } }
The underlying principle of ArrayList collection
The underlying data structure is an array, which is fast in query and slow in addition and deletion.
1. Create an array with default length of 0 at the bottom using the set created by empty parameters.
2. When adding the first element, the underlying layer will create a new array of length 10.
3. When the storage is full, the capacity will be expanded by 1.5 times.
4. If multiple elements are added at once, and 1.5 times cannot be put down, the length of the newly created array shall be subject to actuality.
The underlying principle of LinkedList collection
The underlying data structure is a two-way linked list, which is slow to query and fast to add and delete, but if the operation is the head and tail elements, the speed is also extremely fast.
Therefore, LinkedList itself has many unique APIs that directly operate the head and tail elements. Therefore, in addition to the methods inherited from Collection and List, there are also the following methods
Special method | illustrate |
public void addFirst(E e) | Insert the specified element at the beginning of the list |
public void addLast(E e) | Append the specified element to the end of this list |
public E getFirst() | Returns the first element in this list |
public E getLast() | Returns the last element in this list |
public E removeFirst() | Delete from this list and return the first element |
public E removeLast() | Delete from this list and return the last element |
Summarize
This is the end of this article about the methods and traversal methods of Java collection List. For more related Java collection List content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!