Java中的LinkedList
LinkedList 類擴充套件了 AbstractSequentialList 並實現了 List 介面。它提供了一個連結串列資料結構。
以下是 LinkedList 類支援的建構函式。
| 序號 | 建構函式及描述 |
|---|---|
| 1 | LinkedList( ) 此建構函式構建一個空的連結串列。 |
| 2 | LinkedList(Collection c) 此建構函式構建一個連結串列,該連結串列用集合 c 中的元素初始化。 |
除了從其父類繼承的方法外,LinkedList 還定義了以下方法。
| 序號 | 方法及描述 |
|---|---|
| 1 | void add(int index, Object element) 在此列表的指定位置 index 插入指定的元素。如果指定的 index 超出範圍 (index < 0 || index > size()),則丟擲 IndexOutOfBoundsException。 |
| 2 | boolean add(Object o) 將指定的元素追加到此列表的末尾。 |
| 3 | boolean addAll(Collection c) 將指定集合中的所有元素追加到此列表的末尾,其順序與指定集合的迭代器返回的順序相同。如果指定的集合為空,則丟擲 NullPointerException。 |
| 4 | boolean addAll(int index, Collection c) 將指定集合中的所有元素插入到此列表中,從指定位置開始。如果指定的集合為空,則丟擲 NullPointerException。 |
| 5 | void addFirst(Object o) 在此列表的開頭插入給定的元素。 |
| 6 | void addLast(Object o) 將給定的元素追加到此列表的末尾。 |
| 7 | void clear() 從此列表中刪除所有元素。 |
| 8 | Object clone() 返回此 LinkedList 的淺複製。 |
| 9 | boolean contains(Object o) 如果此列表包含指定的元素,則返回 true。更正式地說,當且僅當此列表至少包含一個元素 e,使得 (o==null ? e==null : o.equals(e)) 時,才返回 true。 |
| 10 | Object get(int index) 返回此列表中指定位置的元素。如果指定的 index 超出範圍 (index < 0 || index >= size()),則丟擲 IndexOutOfBoundsException。 |
| 11 | Object getFirst() 返回此列表中的第一個元素。如果此列表為空,則丟擲 NoSuchElementException。 |
| 12 | Object getLast() 返回此列表中的最後一個元素。如果此列表為空,則丟擲 NoSuchElementException。 |
| 13 | int indexOf(Object o) 返回此列表中指定元素第一次出現的索引,如果列表不包含此元素,則返回 -1。 |
| 14 | int lastIndexOf(Object o) 返回此列表中指定元素最後一次出現的索引,如果列表不包含此元素,則返回 -1。 |
| 15 | ListIterator listIterator(int index) 返回此列表中元素的列表迭代器(按正確順序),從列表中的指定位置開始。如果指定的 index 超出範圍 (index < 0 || index >= size()),則丟擲 IndexOutOfBoundsException。 |
| 16 | Object remove(int index) 從此列表中刪除指定位置的元素。如果此列表為空,則丟擲 NoSuchElementException。 |
| 17 | boolean remove(Object o) 從此列表中刪除指定元素的第一次出現。如果此列表為空,則丟擲 NoSuchElementException。如果指定的 index 超出範圍 (index < 0 || index >= size()),則丟擲 IndexOutOfBoundsException。 |
| 18 | Object removeFirst() 刪除並返回此列表中的第一個元素。如果此列表為空,則丟擲 NoSuchElementException。 |
| 19 | Object removeLast() 刪除並返回此列表中的最後一個元素。如果此列表為空,則丟擲 NoSuchElementException。 |
| 20 | Object set(int index, Object element) 用指定的元素替換此列表中指定位置的元素。如果指定的 index 超出範圍 (index < 0 || index >= size()),則丟擲 IndexOutOfBoundsException。 |
| 21 | int size() 返回此列表中元素的數量。 |
| 22 | Object[] toArray() 返回一個包含此列表中所有元素的陣列,其順序正確。如果指定的陣列為空,則丟擲 NullPointerException。 |
| 23 | Object[] toArray(Object[] a) 返回一個包含此列表中所有元素的陣列,返回陣列的執行時型別與指定陣列的型別相同。 |
示例
以下程式說明了 LinkedList 支援的幾種方法。
import java.util.*;
public class LinkedListDemo {
public static void main(String args[]) {
// create a linked list
LinkedList ll = new LinkedList();
// add elements to the linked list
ll.add("F");
ll.add("B");
ll.add("D");
ll.add("E");
ll.add("C");
ll.addLast("Z");
ll.addFirst("A");
ll.add(1, "A2");
System.out.println("Original contents of ll: " + ll);
// remove elements from the linked list
ll.remove("F");
ll.remove(2);
System.out.println("Contents of ll after deletion: " + ll);
// remove first and last elements
ll.removeFirst();
ll.removeLast();
System.out.println("ll after deleting first and last: " + ll);
// get and set a value
Object val = ll.get(2);
ll.set(2, (String) val + " Changed");
System.out.println("ll after change: " + ll);
}
}輸出
Original contents of ll: [A, A2, F, B, D, E, C, Z] Contents of ll after deletion: [A, A2, D, E, C, Z] ll after deleting first and last: [A2, D, E, C] ll after change: [A2, D, E Changed, C]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP