JAVA------List of collections

List collection overview : inherited from Collection

  1. 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
  2. Allow duplicate elements
  3. ArrayList and LinkedList are inherited from List, so List has methods, they also have
  4. 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

  1. Obtained by the listiterator() method of the List collection, so it is a unique iterator of the List collection
  2. Inherited from Iterator: E next(): returns the next element of the iterator
  3. boolean hasNext(): returns true if the iterator has more elements
  4. E previous(): Return to the previous element of the list
  5. boolean hasPrevious(): returns true if the iterator has more elements while traversing the list in the opposite direction at this time
  6. 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:

  1. public void addFirst(E e): Insert the specified element at the beginning of the list
  2. public void addLast(E e): Append the specified element to the end of this list
  3. public E getFirst(): returns the first element of this list
  4. public E getLast(): returns the last element of this list
  5. public E removeFirst(): Remove and return the first element from this list
  6. 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);
	}
}

Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/113445948