List collection overview : inherited from Collection
- Ordered collection, users can precisely control the insertion position of each element in the list.
Users can access elements by integer index and search for elements in the list - Allow duplicate elements
- ArrayList and LinkedList are inherited from List, so List has methods, they also have
- The underlying data structure of ArrayList is an array, and the underlying data structure of LinkedList is a linked list
List collection specific methods:
4. void add(int index, E element): insert the specified element at the specified position in this collection
5. E remove(int index): delete the element at the specified index, and return the deleted element
6. E set(int index, E element): modify the element at the specified index and return the modified element
7. E get(int index): return the element at the specified index
Example:
package List;
import java.util.ArrayList;
import java.util.List;
public class ListDemo02 {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
//void add(int index,E element)
list.add(1,"javase");
//E remove(int index)
System.out.println(list.remove("hello"));
//E set(int index,E element)
System.out.println(list.set(2,"javaee"));
//E get(int index)
for(int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
System.out.println(list);
}
}
ListIterator: list iterator
- Obtained by the listiterator() method of the List collection, so it is a unique iterator of the List collection
- Inherited from Iterator: E next(): returns the next element of the iterator
- boolean hasNext(): returns true if the iterator has more elements
- E previous(): Return to the previous element of the list
- boolean hasPrevious(): returns true if the iterator has more elements while traversing the list in the opposite direction at this time
- void add(E e): insert the specified element into the list
Example:
package List;
import java.util.*;
public class ListIteatorDemo {
public static void main(String[] args) {
List<String> list=new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
ListIterator<String> lit=list.listIterator();
/* while(lit.hasNext()) {
String s=lit.next();
System.out.println(s);
}
System.out.println("------");
//倒序遍历
while(lit.hasPrevious()) {
String s=lit.previous();
System.out.println(s);
}
*/
while(lit.hasNext()) {
String s=lit.next();
if(s.equals("world")) {
lit.add("javaee");
}
}
System.out.println(list);
}
}
LinkedList unique functions:
- 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 of this list
- public E getLast(): returns the last element of this list
- public E removeFirst(): Remove and return the first element from this list
- public E removeLast(): Remove and return the last element from this list
Example:
package List;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> linkedList=new LinkedList<>();
linkedList.add("hello");
linkedList.add("world");
linkedList.add("java");
linkedList.addFirst("javase");
linkedList.addLast("javaee");
System.out.println(linkedList.getFirst());
System.out.println(linkedList.getLast());
System.out.println(linkedList.removeFirst());
System.out.println(linkedList.removeLast());
System.out.println(linkedList);
}
}