java LinkedList的使用方法介紹
阿新 • • 發佈:2019-02-09
LinkedList類是雙向列表,列表中的每個節點都包含了對前一個和後一個元素的引用.
LinkedList的建構函式如下
1. public LinkedList(): ——生成空的連結串列
2. public LinkedList(Collection col): 複製建構函式
1、獲取連結串列的第一個和最後一個元素
2、獲取連結串列元素
如果不指定索引的話,元素將被新增到連結串列的最後.
public boolean add(Object element)
public boolean add(int index, Object element)
也可以把連結串列當初棧或者佇列來處理:
public boolean addFirst(Object element)
public boolean addLast(Object element)
addLast()方法和不帶索引的add()方法實現的效果一樣.
lList.clear();
10、刪除列表的首位元素
LinkedList的建構函式如下
1. public LinkedList(): ——生成空的連結串列
2. public LinkedList(Collection col): 複製建構函式
1、獲取連結串列的第一個和最後一個元素
- import java.util.LinkedList;
- publicclass LinkedListTest{
- publicstaticvoid main(String[] args) {
-
LinkedList<String> lList = new
- 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);
- }
- 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;
- publicclass LinkedListTest{
- publicstaticvoid 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);
- }
- }
- public Object removeFirst()
- public Object removeLast()
- import java.util.LinkedList;
- publicclass MainClass {
- publicstaticvoid 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);
- }
- }
- import java.util.LinkedList;
- publicclass MainClass {
- publicstaticvoid 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();
- publicvoid push(Object v) {
- list.addFirst(v);
- }
- public Object top() {
- return list.getFirst();
- }
- public Object pop() {
- return list.removeFirst();
- }
- }
- import java.util.LinkedList;
- publicclass MainClass {
- publicstaticvoid 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();
- publicvoid put(Object v) {
- list.addFirst(v);
- }
- public Object get() {
- return list.removeLast();
- }
- publicboolean isEmpty() {
- return list.isEmpty();
- }
- }
8、將LinkedList轉換成ArrayList
- ArrayList<String> arrayList = new ArrayList<String>(linkedList);
- for (String s : arrayList) {
- System.out.println("s = " + s);
- }
lList.clear();
10、刪除列表的首位元素
- import java.util.LinkedList;
- publicclass Main {
- publicstaticvoid 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;
- publicclass Main {
- publicstaticvoid 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;