一、LinkList特有的功能:
public void addFirst(E e)
public void addLast(E e)
public E getFirst()
public E getLast()
public E removeFirst()
public E removeLast()
package com.edu_05;
import java.util.LinkedList;
public class LinkListDemo {
public static void main(String[] args) {
LinkedList<String> strings = new LinkedList<String>();
strings.add("hello");
strings.add("java");
strings.add("c++");
System.out.println(strings);//[hello, java, c++]
//public void addFirst(E e)往首部添加
strings.addFirst("1");
System.out.println(strings);//[1, hello, java, c++]
//public void addLast(E e)
strings.addLast("2");
System.out.println(strings);//[1, hello, java, c++, 2]
//public E getFirst()
String first = strings.getFirst();
System.out.println(first);//1
//public E getLast()
String last = strings.getLast();
System.out.println(last);//2
//public E removeFirst()
String removeFirst = strings.removeFirst();
System.out.println(removeFirst);//1
System.out.println(strings);//[hello, java, c++, 2]
//public E removeLast()
String removeLast = strings.removeLast();
System.out.println(removeLast);//2
System.out.println(strings);//[hello, java, c++]
}
}
三、ArrayList与LinkedList的相同点与不同点
相同点:有顺序的,元素可以重复(1)ArrayList特点:
底层数据结构是数组,查询快,增删慢
线程不安全,效率高
(2)LinkedList特点:
底层数据结构是链表,查询慢,增删快
线程不安全,效率高