队列和堆栈 --- 先进先出和先进后出

private static void queue()//  队列
{
Queue queue  = new LinkedList<>();
queue.offer("Hello");
queue.offer("world");
queue.offer("I m find");
System.out.println("--------------------------");
System.out.println(queue.size());
String aa ;
while((aa = queue.poll()) !=null)
{
System.out.println("queue---"+aa);
}
System.out.println(queue.size());
System.out.println("--------------------------");
}


private static void stack() // 堆栈
{
Vector stack = new Vector();
stack.add("aaa");
stack.add("bbb");
stack.add("ccc");
stack.add("ddd");
stack.add("fff");
System.out.println("---------------------------");
System.out.println("size"+stack.size());

String aa ;
try {
while((aa = stack.lastElement()) !=null)
{
System.out.println("a---"+aa);
stack.removeElement(aa);
//break;
}
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("size"+stack.size());

}


队列和堆栈区别:--- 先进先出和先进后出


System:

--------------------------
3
queue---Hello
queue---world
queue---I m find
0
--------------------------
---------------------------
size5
a---fff
a---ddd
a---ccc
a---bbb
a---aaa
size0

你可能感兴趣的:(Java,stackqueue)