public void abc() { LinkedList mLis = new LinkedList<Integer>(); mLis.add(2); mLis.add(3); mLis.add(4); mLis.offerLast(9); System.out.print(mLis.size()); }
/** 测试LinkedList以双向链表的使用方法 */ private static void testLinkedListDeque() { System.out.println( "Test methods: addFirst(object), addLast(object), offerFirst(), offerLast(), getFirst(object), getLast(object), pollFirst(), pollLast(), peekFirst(), peekLast(), removeFirst(), removeLast()"); // 将 “abcdefg”初始化到LinkedList中 LinkedList<String> list = new LinkedList<String>(); // addFirst/addLast 没有返回值、offerFirst/offerLast返回true、内部都是调用Entry.addBefore()来添加的 list.addFirst("c"); list.offerFirst("b"); list.offerFirst("a"); list.addLast("d"); list.addLast("e"); list.offerLast("f"); printList(list); /* 相同: * getFirst/getLast、peekFirst/peekLast方法都是获取第一个元素、 * 不同: * getXXX方法在list的size为0抛出异常、 * peekXX方法在list的size为0返回null、 */ printStr("list getFirst() obtains element: " + list.getFirst()); printStr("list peekFirst() obtains element: " + list.peekFirst()); printStr("list getLast() obtains element: " + list.getLast()); printStr("list peekLast() obtains element: " + list.peekLast()); printList(list); /* * 相同: * pollXXX、removeXXX都是获取第一个元素、并且将此元素从list中移除 * 不同: * removeXXX方法在list的size为0抛出异常、 * pollXXX方法在list的size为0返回null、 */ printStr(list.pollFirst()); printStr(list.removeFirst()); printList(list); printStr(list.pollLast()); printStr(list.removeLast()); printList(list); }
public static void main(String a[]) { LinkedList<String> arrl = new LinkedList<String>(); arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println(arrl); System.out.println("Adding element at last position..."); arrl.addLast("I am last"); System.out.println(arrl); System.out.println("Adding element at last position..."); arrl.offerLast("I am last - 1"); System.out.println(arrl); System.out.println("Adding element at last position..."); arrl.offer("I am last - 2"); System.out.println(arrl); }