Java 简明教程
Java LinkedList Class
Class declaration
下面是 java.util.LinkedList 类的声明 −
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable
Methods inherited
此类从以下类中继承方法:
-
java.util.AbstractSequentialList
-
java.util.AbstractList
-
java.util.AbstractCollection
-
java.util.Object
-
java.util.List
-
java.util.Deque
Adding element to a LinkedList Example
以下示例展示了 Java LinkedList add(E) 方法的使用,用它来添加整数。我们通过对每个元素使用 add() 方法调用将两个整数添加到 LinkedList 对象,然后打印每个元素以显示添加的元素。
package com.tutorialspoint;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
// create an empty linked list
LinkedList<Integer> linkedList = new LinkedList<>();
// use add() method to add elements in the linkedList
linkedList.add(20);
linkedList.add(30);
linkedList.add(20);
linkedList.add(30);
linkedList.add(15);
linkedList.add(22);
linkedList.add(11);
// let us print all the elements available in linkedList
for (Integer number : linkedList) {
System.out.println("Number = " + number);
}
}
}