Java LinkedList基本用法

时间:2024-11-17 17:30:07

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。

LinkedList类是双向列表,列表中的每个节点都包含了对前一个和后一个元素的引用.
LinkedList的构造函数如下:

  • public LinkedList():  ——生成空的链表
  • public LinkedList(Collection col):  复制构造函数

1、获取链表的第一个和最后一个元素

import ;
 
public class LinkedListTest{
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    ("1");
    ("2");
    ("3");
    ("4");
    ("5");
 
 
    ("链表的第一个元素是 : " + ());
    ("链表最后一个元素是 : " + ());
  }
}

2、获取链表元素  

for (String str: lList) {
   (str);
}

3、从链表生成子表

List subl = (1, 4);
    (subl);
    (2);
    (lst);
    (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 ;
 
public class LinkedListTest{
  public static void main(String[] a) {
    LinkedList list = new LinkedList();
    ("A");
    ("B");
    ("C");
    ("D");
    ("X");
    ("Z");
    (list);
  }
}

 5、删除元素

public Object removeFirst()
public Object removeLast()
import ;
 
 
public class MainClass {
  public static void main(String[] a) {
 
 
    LinkedList list = new LinkedList();
    ("A");
    ("B");
    ("C");
    ("D");
    ();
    ();
    (list);
  }
}

6、使用链表实现栈效果

import ;
public class MainClass {
  public static void main(String[] args) {
    StackL stack = new StackL();
    for (int i = 0; i < 10; i++)
      (i);
    (());
    (());
    (());
    (());
    (());
  }
}
class StackL {
  private LinkedList list = new LinkedList();
  public void push(Object v) {
    (v);
  }
  public Object top() {
    return ();
  }
  public Object pop() {
    return ();
  }
}

7、使用链表来实现队列效果

import ;
public class MainClass {
  public static void main(String[] args) {
    Queue queue = new Queue();
    for (int i = 0; i < 10; i++)
      ((i));
    while (!())
      (());
  }
}
class Queue {
  private LinkedList list = new LinkedList();
  public void put(Object v) {
    (v);
  }
  public Object get() {
    return ();
  }
  public boolean isEmpty() {
    return ();
  }
}

8、将LinkedList转换成ArrayList

ArrayList<String> arrayList = new ArrayList<String>(linkedList);
    for (String s : arrayList) {
      ("s = " + s);
    }

9、删掉所有元素:清空LinkedList   

 ();

10、删除列表的首位元素

import ;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    ("1");
    ("2");
    ("3");
    ("4");
    ("5");
    (lList);
        //元素在删除的时候,仍然可以获取到元素
    Object object = ();
    (object + " has been removed");
    (lList);
    object = ();
    (object + " has been removed");
    (lList);
  }
}

11、根据范围删除列表元素

import ;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    ("1");
    ("2");
    ("3");
    ("4");
    ("5");
    (lList);
    (2, 5).clear();
    (lList);
  }
}

12、删除链表的特定元素

import ;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    ("1");
    ("2");
    ("3");
    ("4");
    ("5");
    (lList);
    (("2"));//删除元素值=2的元素
    (lList);
    Object obj = (2);  //删除第二个元素
    (obj + " 已经从链表删除");
    (lList);
  }
}

13、将LinkedList转换为数组,数组长度为0

import ;
import ;
public class Main {
  public static void main(String[] args) {
    List<String> theList = new LinkedList<String>();
    ("A");
    ("B");
    ("C");
    ("D");
    String[] my = (new String[0]);
    for (int i = 0; i < ; i++) {
      (my[i]);
    }
  }
}

14、将LinkedList转换为数组,数组长度为链表长度

import ;
import ;
public class Main {
  public static void main(String[] args) {
    List<String> theList = new LinkedList<String>();
    ("A");
    ("B");
    ("C");
    ("D");
    String[] my = (new String[()]);
    for (int i = 0; i < ; i++) {
      (my[i]);
    }
  }
}

15、将LinkedList转换成ArrayList

import ;
import ;
import ;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> myQueue = new LinkedList<String>();
    ("A");
    ("B");
    ("C");
    ("D");
    List<String> myList = new ArrayList<String>(myQueue);
    for (Object theFruit : myList)
      (theFruit);
  }
}

16、实现栈

import ;
import ;
public class Main {
  public static void main(String[] argv) throws Exception {
    LinkedList stack = new LinkedList();
    Object object = "";
    (object);
    Object o = ();
    stack = (LinkedList) (stack);
  }
}

17、实现队列

import ;
public class Main {
  public static void main(String[] argv) throws Exception {
    LinkedList queue = new LinkedList();
    Object object = "";
    // Add to end of queue
    (object);
    // Get head of queue
    Object o = ();
  }
}

18 、同步方法

import ;
import ;
public class Main {
  public static void main(String[] argv) throws Exception {
    LinkedList queue = new LinkedList();
    Object object = "";
    (object);
    Object o = ();
    queue = (LinkedList) (queue);
  }
}

19、查找元素位置

import ;
 
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    ("1");
    ("2");
    ("3");
    ("4");
    ("5");
    ("2");
    (("2"));
    (("2"));
  }
}

20、替换元素

import ;
 
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    ("1");
    ("2");
    ("3");
    ("4");
    ("5");
    (lList);
    (3, "Replaced");//使用set方法替换元素,方法的第一个参数是元素索引,后一个是替换值
    (lList);
  }
}

21、链表添加对象

import ;
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>();
    (new Address("A", "11 Ave", "U", "IL", "11111"));
    (new Address("R", "11 Lane", "M", "IL", "22222"));
    (new Address("T", "8 St", "C", "IL", "33333"));
    for (Address element : ml)
      (element + "\n");
  }
}

22、确认链表是否存在特定元素

import ;
 
 
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    ("1");
    ("2");
    ("3");
    ("4");
    ("5");
    if (("4")) {
      ("LinkedList contains 4");
    } else {
      ("LinkedList does not contain 4");
    }
  }
}

23、根据链表元素生成对象数组

Object[] objArray = ();
for (Object obj: objArray) {
   (obj);
}

24、链表多线程

import ;
import ;
import ;
class PrepareProduction implements Runnable {
  private final List<String> queue;
  PrepareProduction(List<String> q) {
    queue = q;
  }
  public void run() {
    ("1");
    ("done");
  }
}
class DoProduction implements Runnable {
  private final List<String> queue;
  DoProduction(List<String> q) {
    queue = q;
  }
  public void run() {
    String value = (0);
    while (!("*")) {
      (value);
      value = (0);
    }
  }
}
public class Main {
  public static void main(String[] args) throws Exception {
    List q = (new LinkedList<String>());
    Thread p1 = new Thread(new PrepareProduction(q));
    Thread c1 = new Thread(new DoProduction(q));
    ();
    ();
    ();
    ();
  }
}

25、优先级链表(来自JBOSS)

import ;
import ;
import ;
import ;
import ;
 
 
public class BasicPriorityLinkedList {
 
 
  protected LinkedList[] linkedLists;
  protected int priorities;
  protected int size;
 
 
  public BasicPriorityLinkedList(int priorities) {
     = priorities;
    initDeques();
  }
  public void addFirst(Object obj, int priority) {
    linkedLists[priority].addFirst(obj);
    size++;
  }
  public void addLast(Object obj, int priority) {
    linkedLists[priority].addLast(obj);
    size++;
  }
  public Object removeFirst() {
    Object obj = null;
    for (int i = priorities - 1; i >= 0; i--) {
      LinkedList ll = linkedLists[i];
      if (!()) {
        obj = ();
        break;
      }
    }
    if (obj != null) {
      size--;
    }
    return obj;
  }
  public Object removeLast() {
    Object obj = null;
    for (int i = 0; i < priorities; i++) {
      LinkedList ll = linkedLists[i];
      if (!()) {
        obj = ();
      }
      if (obj != null) {
        break;
      }
    }
    if (obj != null) {
      size--;
    }
    return obj;
  }
 
 
  public Object peekFirst() {
    Object obj = null;
    for (int i = priorities - 1; i >= 0; i--) {
      LinkedList ll = linkedLists[i];
      if (!()) {
        obj = ();
      }
      if (obj != null) {
        break;
      }
    }
    return obj;
  }
 
 
  public List getAll() {
    List all = new ArrayList();
    for (int i = priorities - 1; i >= 0; i--) {
      LinkedList deque = linkedLists[i];
      (deque);
    }
    return all;
  }
 
 
  public void clear() {
    initDeques();
  }
 
 
  public int size() {
    return size;
  }
 
 
  public boolean isEmpty() {
    return size == 0;
  }
 
 
  public ListIterator iterator() {
    return new PriorityLinkedListIterator(linkedLists);
  }
 
 
  protected void initDeques() {
    linkedLists = new LinkedList[priorities];
    for (int i = 0; i < priorities; i++) {
      linkedLists[i] = new LinkedList();
    }
    size = 0;
  }
 
 
  class PriorityLinkedListIterator implements ListIterator {
    private LinkedList[] lists;
    private int index;
    private ListIterator currentIter;
    PriorityLinkedListIterator(LinkedList[] lists) {
       = lists;
      index =  - 1;
      currentIter = lists[index].listIterator();
    }
 
 
    public void add(Object arg0) {
      throw new UnsupportedOperationException();
    }
 
 
    public boolean hasNext() {
      if (()) {
        return true;
      }
      while (index >= 0) {
        if (index == 0 || ()) {
          break;
        }
        index--;
        currentIter = lists[index].listIterator();
      }
      return ();
    }
 
 
    public boolean hasPrevious() {
      throw new UnsupportedOperationException();
    }
 
 
    public Object next() {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      return ();
    }
 
 
    public int nextIndex() {
      throw new UnsupportedOperationException();
    }
 
 
    public Object previous() {
      throw new UnsupportedOperationException();
    }
 
 
    public int previousIndex() {
      throw new UnsupportedOperationException();
    }
 
 
    public void remove() {
      ();
      size--;
    }
 
 
    public void set(Object obj) {
      throw new UnsupportedOperationException();
    }
  }
 
 
}

26、生成list的帮助类(来自google)

import ;
import ;
import ;
import ;
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();
    (set, elements);
    return set;
  }
  public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) {
    ArrayList<E> list = newArrayList();
    for(E e : elements) {
      (e);
    }
    return list;
  }
  public static <E> LinkedList<E> newLinkedList() {
    return new LinkedList<E>();
  }
}