一起Talk Android吧(第八十六回:Java中的类集之Queue)

各位看官们,大家好,上一回中咱们说的是Java中类集之List的例子,这一回咱们说的例子是Java中的类集之Queue。闲话休提,言归正转。让我们一起Talk Android吧!

看官们,我们在前面章回中对Java中的类集做了概述性的介绍,这一回中我们将对类集中具体的接口和类进行介绍,这一回主要介绍Queue接口和它的实现类LinkedList

这个Queue就是我们在数据结构中的队列。QueueCollection接口的子接口,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同时实现了ListQueue接口,因此可以把它当作这两种数据结构来使用。不过它也实现了部分自己特有的方法,因此可以把它当作栈来做。在程序中可以依据程序的需要来转换它或者不转换它直接使用。下面是程序的运行结果,请参考:

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的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!


猜你喜欢

转载自blog.csdn.net/talk_8/article/details/81037701