LinkedList類是雙向列表,列表中的每一個節點都包含了對前一個和後一個元素的引用.
LinkedList的構造函數以下
1. public LinkedList(): ——生成空的鏈表
2. public LinkedList(Collection col): 複製構造函數
一、獲取鏈表的第一個和最後一個元素java
- 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());
- }
- }
二、獲取鏈表元素 數組
- for (String str: lList) {
- System.out.println(str);
- }
三、從鏈表生成子表多線程
- List subl = lList.subList(1, 4);
- System.out.println(subl);
- lst.remove(2);
- System.out.println(lst);
- System.out.println(lList);
四、添加元素:添加單個元素
若是不指定索引的話,元素將被添加到鏈表的最後.
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);
- }
- }
五、刪除元素ui
- 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);
- }
- }
六、使用鏈表實現棧效果this
- 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();
- }
- }
七、使用鏈表來實現隊列效果google
- 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();
- }
- }
八、將LinkedList轉換成ArrayListspa
- ArrayList<String> arrayList = new ArrayList<String>(linkedList);
- for (String s : arrayList) {
- System.out.println("s = " + s);
- }
九、刪掉全部元素:清空LinkedList
lList.clear();
十、刪除列表的首位元素.net
- 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);
- }
- }
十一、根據範圍刪除列表元素線程
- 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);
- lList.subList(2, 5).clear();
- System.out.println(lList);
- }
- }
十二、刪除鏈表的特定元素
- 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);
- System.out.println(lList.remove("2"));
- System.out.println(lList);
- Object obj = lList.remove(2);
- System.out.println(obj + " 已經從鏈表刪除");
- System.out.println(lList);
- }
- }
1三、將LinkedList轉換爲數組,數組長度爲0
- import java.util.LinkedList;
- import java.util.List;
- public class Main {
- public static void main(String[] args) {
- List<String> theList = new LinkedList<String>();
- theList.add("A");
- theList.add("B");
- theList.add("C");
- theList.add("D");
- String[] my = theList.toArray(new String[0]);
- for (int i = 0; i < my.length; i++) {
- System.out.println(my[i]);
- }
- }
- }
1四、將LinkedList轉換爲數組,數組長度爲鏈表長度
- import java.util.LinkedList;
- import java.util.List;
- public class Main {
- public static void main(String[] args) {
- List<String> theList = new LinkedList<String>();
- theList.add("A");
- theList.add("B");
- theList.add("C");
- theList.add("D");
- String[] my = theList.toArray(new String[theList.size()]);
- for (int i = 0; i < my.length; i++) {
- System.out.println(my[i]);
- }
- }
- }
1五、將LinkedList轉換成ArrayList
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- public class Main {
- public static void main(String[] args) {
- LinkedList<String> myQueue = new LinkedList<String>();
- myQueue.add("A");
- myQueue.add("B");
- myQueue.add("C");
- myQueue.add("D");
- List<String> myList = new ArrayList<String>(myQueue);
- for (Object theFruit : myList)
- System.out.println(theFruit);
- }
- }
1六、實現棧
- import java.util.Collections;
- import java.util.LinkedList;
- public class Main {
- public static void main(String[] argv) throws Exception {
- LinkedList stack = new LinkedList();
- Object object = "";
- stack.addFirst(object);
- Object o = stack.getFirst();
- stack = (LinkedList) Collections.synchronizedList(stack);
- }
- }
1七、實現隊列
- import java.util.LinkedList;
- public class Main {
- public static void main(String[] argv) throws Exception {
- LinkedList queue = new LinkedList();
- Object object = "";
-
- queue.add(object);
-
- Object o = queue.removeFirst();
- }
- }
18 、同步方法
- import java.util.Collections;
- import java.util.LinkedList;
- public class Main {
- public static void main(String[] argv) throws Exception {
- LinkedList queue = new LinkedList();
- Object object = "";
- queue.add(object);
- Object o = queue.removeFirst();
- queue = (LinkedList) Collections.synchronizedList(queue);
- }
- }
1九、查找元素位置
- 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");
- lList.add("2");
- System.out.println(lList.indexOf("2"));
- System.out.println(lList.lastIndexOf("2"));
- }
- }
20、替換元素
- 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);
- lList.set(3, "Replaced");
- System.out.println(lList);
- }
- }
2一、鏈表添加對象
- import java.util.LinkedList;
- class Address {
- private String name;
- private String street;
- private String city;
- private String state;
- private String code;
- Address(String n, String s, String c, String st, String cd) {
- name = n;
- street = s;
- city = c;
- state = st;
- code = cd;
- }
- public String toString() {
- return name + " " + street + " " + city + " " + state + " " + code;
- }
- }
-
-
- class MailList {
- public static void main(String args[]) {
- LinkedList<Address> ml = new LinkedList<Address>();
- ml.add(new Address("A", "11 Ave", "U", "IL", "11111"));
- ml.add(new Address("R", "11 Lane", "M", "IL", "22222"));
- ml.add(new Address("T", "8 St", "C", "IL", "33333"));
- for (Address element : ml)
- System.out.println(element + "\n");
- }
- }
2二、確認鏈表是否存在特定元素
- 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");
- if (lList.contains("4")) {
- System.out.println("LinkedList contains 4");
- } else {
- System.out.println("LinkedList does not contain 4");
- }
- }
- }
2三、根據鏈表元素生成對象數組
- Object[] objArray = lList.toArray();
- for (Object obj: objArray) {
- System.out.println(obj);
- }
2四、鏈表多線程
- import java.util.Collections;
- import java.util.LinkedList;
- import java.util.List;
- class PrepareProduction implements Runnable {
- private final List<String> queue;
- PrepareProduction(List<String> q) {
- queue = q;
- }
- public void run() {
- queue.add("1");
- queue.add("done");
- }
- }
- class DoProduction implements Runnable {
- private final List<String> queue;
- DoProduction(List<String> q) {
- queue = q;
- }
- public void run() {
- String value = queue.remove(0);
- while (!value.equals("*")) {
- System.out.println(value);
- value = queue.remove(0);
- }
- }
- }
- public class Main {
- public static void main(String[] args) throws Exception {
- List q = Collections.synchronizedList(new LinkedList<String>());
- Thread p1 = new Thread(new PrepareProduction(q));
- Thread c1 = new Thread(new DoProduction(q));
- p1.start();
- c1.start();
- p1.join();
- c1.join();
- }
- }
2五、優先級鏈表(來自JBOSS)
2六、生成list的幫助類(來自google)
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.LinkedList;
- import java.util.List;
- public class Lists {
- private Lists() { }
- public static <E> ArrayList<E> newArrayList() {
- return new ArrayList<E>();
- }
- public static <E> ArrayList<E> newArrayListWithCapacity(int initialCapacity) {
- return new ArrayList<E>(initialCapacity);
- }
- public static <E> ArrayList<E> newArrayList(E... elements) {
- ArrayList<E> set = newArrayList();
- Collections.addAll(set, elements);
- return set;
- }
- public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) {
- ArrayList<E> list = newArrayList();
- for(E e : elements) {
- list.add(e);
- }
- return list;
- }
- public static <E> LinkedList<E> newLinkedList() {
- return new LinkedList<E>();
- }
- }