LinkedList類是雙向列表,列表中的每個節點都包含了對前一個和後一個元素的引用.
LinkedList的構造函數如下
1. public LinkedList(): ——生成空的鏈表
2. public LinkedList(Collection col): 復制構造函數
1、獲取鏈表的第一個和最後一個元素
import java.util.LinkedList;
public class LinkedListTest{
public static void main(String[] args) {
LinkedList<String> lList = new LinkedList<String>();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("鏈表的第一個元素是 : " + lList.getFirst());
System.out.println("鏈表最後一個元素是 : " + lList.getLast());
}
}
2、獲取鏈表元素
for (String str: lList) {
System.out.println(str);
}
3、從鏈表生成子表
List subl = lList.subList(1, 4);
System.out.println(subl);
lst.remove(2);
System.out.println(lst);
System.out.println(lList);
List subl = lList.subList(1, 4);
System.out.println(subl);
lst.remove(2);
System.out.println(lst);
System.out.println(lList);
4、添加元素:添加單個元素
如果不指定索引的話,元素將被添加到鏈表的最後.
public boolean add(Object element)
public boolean add(int index, Object element)
也可以把鏈表當初棧或者隊列來處理:
public boolean addFirst(Object element)
public boolean addLast(Object element)
addLast()方法和不帶索引的add()方法實現的效果一樣.
import java.util.LinkedList;
public class LinkedListTest{
public static void main(String[] a) {
LinkedList list = new LinkedList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.addFirst("X");
list.addLast("Z");
System.out.println(list);
}
}
5、刪除元素
public Object removeFirst()
public Object removeLast()
import java.util.LinkedList;
public class MainClass {
public static void main(String[] a) {
LinkedList list = new LinkedList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.removeFirst();
list.removeLast();
System.out.println(list);
}
}
public Object removeFirst()
public Object removeLast()
import java.util.LinkedList;
public class MainClass {
public static void main(String[] a) {
LinkedList list = new LinkedList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.removeFirst();
list.removeLast();
System.out.println(list);
}
}
6、使用鏈表實現棧效果
import java.util.LinkedList;
public class MainClass {
public static void main(String[] args) {
StackL stack = new StackL();
for (int i = 0; i < 10; i++)
stack.push(i);
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}
class StackL {
private LinkedList list = new LinkedList();
public void push(Object v) {
list.addFirst(v);
}
public Object top() {
return list.getFirst();
}
public Object pop() {
return list.removeFirst();
}
}
7、使用鏈表來實現隊列效果
import java.util.LinkedList;
public class MainClass {
public static void main(String[] args) {
Queue queue = new Queue();
for (int i = 0; i < 10; i++)
queue.put(Integer.toString(i));
while (!queue.isEmpty())
System.out.println(queue.get());
}
}
class Queue {
private LinkedList list = new LinkedList();
public void put(Object v) {
list.addFirst(v);
}
public Object get() {
return list.removeLast();
}
public boolean isEmpty() {
return list.isEmpty();
}
}
8、將LinkedList轉換成ArrayList
ArrayList<String> arrayList = new ArrayList<String>(linkedList);
for (String s : arrayList) {
System.out.println("s = " + s);
}
ArrayList<String> arrayList = new ArrayList<String>(linkedList);
for (String s : arrayList) {
System.out.println("s = " + s);
}
9、刪掉所有元素:清空LinkedList
lList.clear();
10、刪除列表的首位元素
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> lList = new LinkedList<String>();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println(lList);
//元素在刪除的時候,仍然可以獲取到元素
Object object = lList.removeFirst();
System.out.println(object + " has been removed");
System.out.println(lList);
object = lList.removeLast();
System.out.println(object + " has been removed");
System.out.println(lList);
}
}
更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2015-07/120219p2.htm