import java.util.Objects;
/**
* Created by kaizige on 2017/7/3.
*/
public class Stack {
private Object[] eles;
private int size=0 ;
private static final int CAPATY=10 ;
public Stack (){
eles=new Object[CAPATY];
}
public void push (Object o){
eles[size++]=o;
}
public Object pop (){
if (size==0 ){
throw new RuntimeException("没有元素" );
}
Object o=eles[--size];
eles[size]=null ;
return o;
}
public static void main (String[] args) {
Stack s=new Stack();
s.push("hello" );
System.out.println(s.pop());
}
}
public class Stack2<E> {
private E[] eles;
private int size=0 ;
private static final int CAPATY=10 ;
@SuppressWarnings("unchecked" )
public Stack2 (){
eles=(E[])new Object[CAPATY];
}
public void push (E o){
eles[size++]=o;
}
public E pop (){
if (size==0 ){
throw new RuntimeException("没有元素" );
}
E o=eles[--size];
eles[size]=null ;
return o;
}
public static void main (String[] args) {
Stack2<String> s=new Stack2<String>();
s.push("hello" );
System.out .println(s.pop().getClass().getCanonicalName());
}
}
public class Stack3<E> {
private Object[] eles;
private int size=0 ;
private static final int CAPATY=10 ;
@SuppressWarnings("unchecdked" )
public Stack3 (){
eles=new Object[CAPATY];
}
public void push (E o){
eles[size++]=o;
}
public E pop (){
if (size==0 ){
throw new RuntimeException("没有元素" );
}
E o=(E) eles[--size];
eles[size]=null ;
return o;
}
public static void main (String[] args) {
Stack2<Integer> s=new Stack2<Integer>();
s.push(2 );
System.out .println(s.pop().getClass().getCanonicalName());
}
}