各位看官們,大家好,上一回中咱們說的是Java中類集之List的例子,這一回咱們說的例子是Java中的類集之Queue
。閒話休提,言歸正轉。讓我們一起Talk Android吧!
看官們,我們在前面章回中對Java中的類集做了概述性的介紹,這一回中我們將對類集中具體的接口和類進行介紹,這一回主要介紹Queue
接口和它的實現類LinkedList
。
這個Queue
就是我們在數據結構中的隊列。Queue
是Collection
接口的子接口,LinkedList
是它的實現類,不過它也是List
接口的實現類,接下來我們看看如何使用它們。
看官們,對隊列的操作可以簡單概括爲:入隊,出隊。
- 入隊的方法:
add(obj)/offer(obj)
- 出隊的方法:
remove()/poll()
- 另外還有兩個出隊方法:
peek()和element()
,
與前面的出隊方法相比,它倆只是用來找到鏈表的頭,但是不刪除頭部的元素。
除此之外,LinkedList
還支持List中的添加和刪除操作,使用的方法也是一樣的。
- 添加鏈表中元素的方法:
add(obj), add(index,obj)
- 刪除鏈表中元素的方法:
remove(index);remove(obj)
LinkList
除了實現List和Queue接口的方法外,還自己添加了一些新的方法,我們可以使用這些方法來實現棧操作.
- 入棧方法:
addFirst(obj)/push(obj)
- 出棧方法:
removeFirst()/pop()
這些方法中有一個obj
參數,它表示列表中的元素。接下來我們通過具體的代碼來介紹如何使用這些方法來對操作列表。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class QueueEx {
public static void main(String args[]){
// init LinkedLisst
// List<Integer> list = new LinkedList<>();
// Queue<Integer> list = new LinkedList<>();
LinkedList<Integer> list = new LinkedList<>();
for(int i=0; i<10;++i){
//list.add(new Integer(i+1));
list.add((i+1));
}
//show size of list ,and content of list
System.out.println("size of list: "+list.size());
for(Integer i:list)
System.out.print(i+" ");
System.out.println();
System.out.println("list: "+list);
//change list to array
Integer [] array = list.toArray(new Integer [] {});
System.out.println("change list to array: "+Arrays.toString(array));
//enqueue operation of queue
list.offer(6);
//list.add(6);
System.out.println("enqueue: "+list);
//dequeue operation of queue
list.poll();
//list.remove();
System.out.println("dequeue: "+list);
System.out.println("list peek :"+list.peek());
System.out.println("list element:"+list.element());
System.out.println("after peek and element: "+list);
//push operation of stack
//list.addFirst(1);
list.push(1);
System.out.println("push: "+list);
//pop operation of stack
//list.removeFirst();
list.pop();
System.out.println("pop: "+list);
//other operation of list
list.addLast(1);
System.out.println("add 1 into the last of list: "+list);
list.removeLast();
System.out.println("del 1 at the last of list: "+list);
//delete the content of list,this is based on content of list
list.remove(new Integer(9));
System.out.println("after removing the content 9: "+list);
//delete the content of list,this is based on location of list
list.remove(6);
System.out.println("after removing the 6th content: "+list);
//add content operation ,it is the same as list
list.add(0, 1);
System.out.println("add 1 into the last of list: "+list);
}
}
看官們,LinkedList
同時實現了List
和Queue
接口,因此可以把它當作這兩種數據結構來使用。不過它也實現了部分自己特有的方法,因此可以把它當作棧來做。在程序中可以依據程序的需要來轉換它或者不轉換它直接使用。下面是程序的運行結果,請參考:
size of list: 10
1 2 3 4 5 6 7 8 9 10
list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
change list to array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
enqueue: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 6]
dequeue: [2, 3, 4, 5, 6, 7, 8, 9, 10, 6]
list peek :2
list element:2
after peek and element: [2, 3, 4, 5, 6, 7, 8, 9, 10, 6]
push: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 6]
pop: [2, 3, 4, 5, 6, 7, 8, 9, 10, 6]
add 1 into the last of list: [2, 3, 4, 5, 6, 7, 8, 9, 10, 6, 1]
del 1 at the last of list: [2, 3, 4, 5, 6, 7, 8, 9, 10, 6]
after removing the content 9: [2, 3, 4, 5, 6, 7, 8, 10, 6]
after removing the 6th content: [2, 3, 4, 5, 6, 7, 10, 6]
add 1 into the last of list: [1, 2, 3, 4, 5, 6, 7, 10, 6]
各位看官,關於Java中類集之Queue
的例子咱們就介紹到這裏,欲知後面還有什麼例子,且聽下回分解!