java实现栈
Posted 大数据最好
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实现栈相关的知识,希望对你有一定的参考价值。
/**
* Created by murphy on 2016/7/4.
*/
public class Stack
private int DEFAULT_SIZE=12;
private int[] arr;
private int count=0;
public Stack(int size)
arr=new int[size];
public Stack()
arr=new int[DEFAULT_SIZE];
public void push(int val)
arr[count++]=val;
public int pop()
int temp=arr[count-1];
count--;
return temp;
public int peek()
return arr[count-1];
public int size()
return count;
public boolean isEmpty()
return count == 0;
public static void main(String[] args)
Stack stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println(stack.peek());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.size());
System.out.println(stack.isEmpty());
System.out.println(stack.pop());
System.out.println(stack.isEmpty());
以上是关于java实现栈的主要内容,如果未能解决你的问题,请参考以下文章