List 的直接子类
构造方法:
LinkedList() 构建一个空列表
LinkedList(Collection <? extends E> c) 构建一个包含指定Collection的元素列表
常用方法演示
import java.util.LinkedList; public class LinkedListDemo { public static void main (String[] args) { LinkedList<String> list = new LinkedList<String>(); //add() list.add("1"); list.add("2"); list.add("3"); traverse(list); list.addFirst("0"); list.addLast("4"); traverse(list); //print the head System.out.println(list.element()); //only print head traverse(list); //not remove the head //get head and last System.out.println(list.getFirst()+" "+list.getLast()); //only print traverse(list); //offer() list.offer("5"); //add 5 traverse(list); list.offerFirst("-1"); list.offerLast("6"); traverse(list); //poll //will remove the element System.out.println(list.pollFirst()+" "+list.pollLast()); traverse(list); //remove the element //pop System.out.println(list.pop()); traverse(list); //remove the head //push list.push("0"); traverse(list); //remove() list.removeFirst(); //remove the head list.removeLast(); //remove the last traverse(list); list.remove(); //remove the head traverse(list); list.remove(1); //remove the element at 1 traverse(list); //other list.add(1,"a"); //add "a" at 1 traverse(list); list.removeFirstOccurrence("a"); //remove the first "a" traverse(list); list.removeLastOccurrence("4"); //remove the last 4 traverse(list); } public static void traverse(LinkedList<String> list) { //遍历 for(int i=0;i<list.size();i++) { System.out.print(list.get(i)+" "); } System.out.println(); } }