LinkedList类常用方法测试
package list;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] srgs) {
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.addFirst(0);
linkedList.add(1);
linkedList.add(2, 2);
linkedList.addLast(3);
System.out.println("LinkedList(直接输出的): " + linkedList);
System.out.println("getFirst()获得第一个元素: " + linkedList.getFirst());
System.out.println("getLast()获得第最后一个元素: " + linkedList.getLast());
System.out.println("removeFirst()删除第一个元素并返回: " + linkedList.removeFirst());
System.out.println("removeLast()删除最后一个元素并返回: " + linkedList.removeLast());
System.out.println("After remove:" + linkedList);
System.out.println("contains()方法判断列表是否包含1这个元素:" + linkedList.contains(1));
System.out.println("该linkedList的大小 : " + linkedList.size());
System.out.println("-----------------------------------------");
linkedList.set(1, 3);
System.out.println("After set(1, 3):" + linkedList);
System.out.println("get(1)获得指定位置(这里为1)的元素: " + linkedList.get(1));
System.out.println("-----------------------------------------");
linkedList.add(3);
System.out.println("indexOf(3): " + linkedList.indexOf(3));
System.out.println("lastIndexOf(3): " + linkedList.lastIndexOf(3));
System.out.println("-----------------------------------------");
System.out.println("peek(): " + linkedList.peek());
System.out.println("element(): " + linkedList.element());
linkedList.poll();
System.out.println("After poll():" + linkedList);
linkedList.remove();
System.out.println("After remove():" + linkedList);
linkedList.offer(4);
System.out.println("After offer(4):" + linkedList);
System.out.println("-----------------------------------------");
linkedList.offerFirst(2);
System.out.println("After offerFirst(2):" + linkedList);
linkedList.offerLast(5);
System.out.println("After offerLast(5):" + linkedList);
System.out.println("peekFirst(): " + linkedList.peekFirst());
System.out.println("peekLast(): " + linkedList.peekLast());
linkedList.pollFirst();
System.out.println("After pollFirst():" + linkedList);
linkedList.pollLast();
System.out.println("After pollLast():" + linkedList);
linkedList.push(2);
System.out.println("After push(2):" + linkedList);
linkedList.pop();
System.out.println("After pop():" + linkedList);
linkedList.add(3);
linkedList.removeFirstOccurrence(3);
System.out.println("After removeFirstOccurrence(3):" + linkedList);
linkedList.removeLastOccurrence(3);
System.out.println("After removeFirstOccurrence(3):" + linkedList);
System.out.println("-----------------------------------------");
linkedList.clear();
for (int i = 0; i < 100000; i++) {
linkedList.add(i);
}
long start = System.currentTimeMillis();
Iterator<Integer> iterator = linkedList.iterator();
while (iterator.hasNext()) {
iterator.next();
}
long end = System.currentTimeMillis();
System.out.println("Iterator:" + (end - start) + " ms");
start = System.currentTimeMillis();
for (int i = 0; i < linkedList.size(); i++) {
linkedList.get(i);
}
end = System.currentTimeMillis();
System.out.println("for:" + (end - start) + " ms");
start = System.currentTimeMillis();
for (Integer i : linkedList)
;
end = System.currentTimeMillis();
System.out.println("for2:" + (end - start) + " ms");
LinkedList<Integer> temp1 = new LinkedList<>();
temp1.addAll(linkedList);
start = System.currentTimeMillis();
while (temp1.size() != 0) {
temp1.pollFirst();
}
end = System.currentTimeMillis();
System.out.println("pollFirst()或pollLast():" + (end - start) + " ms");
LinkedList<Integer> temp2 = new LinkedList<>();
temp2.addAll(linkedList);
start = System.currentTimeMillis();
while (temp2.size() != 0) {
temp2.removeFirst();
}
end = System.currentTimeMillis();
System.out.println("removeFirst()或removeLast():" + (end - start) + " ms");
}
}