1.不用linkedlist,实现自己的内部链式存储机制
public class LinkedStack<T> {
private class Node{
private T item;
private Node next;
public Node(T item ,Node next){
this.item=item;
this.next=next;
}
public Node() {
super();
}
public T getItem() {
return item;
}
public void setItem(T item) {
this.item = item;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public boolean isEnd() {
return this.item==null&&this.next==null;
}
}
private Node top=new Node();
public void push(T value) {
top=new Node(value,top);
}
public T pop() {
T result=top.item;
if(!top.isEnd()) {
top=top.getNext();
}
return result;
}
}
2.java泛型有一个局限性,不能以基本类型作为类型参数,不过java具备了自动打包和拆包的功能,可以很方便的在基本数据类型和相应的包装类型之间进行转换。