用java来实现泛型的顺序栈。
/** * 顺序栈 * @param泛型T */public class SequenceStack { private T[] data; private int top; /** * 初始化 * * @param capacity * 栈的大小 */ public SequenceStack(int capacity) { data = (T[]) new Object[capacity]; top = -1; } /** * 是否为空 * * @return */ public boolean isEmpty() { if (top == -1) { return true; } else { return false; } } /** * 是否满 * * @return */ public boolean isFull() { if (top != -1 && top == data.length - 1) { return true; } else { return false; } } /** * 压栈 * * @param t * @throws Exception */ public void push(T t) throws Exception { if (isFull()) { throw new Exception("push into full stack exception"); } top++; data[top] = t; } /** * 退栈 * * @param t * @return * @throws Exception */ public T pop() throws Exception { if (isEmpty()) { throw new Exception("pop empty stack exception"); } T result = data[top]; top--; return result; } /** * 返回指定下标的元素 * * @param t * @return * @throws Exception */ public T get(int index) throws Exception { if (index > top || index < 0) { throw new Exception("out of size exception"); } return data[top]; } public static String DexStringToBinary(int hex) throws Exception { SequenceStack stack = new SequenceStack (10); while (hex > 0) { int i = hex % 2; hex = hex / 2; stack.push(i); } String result = ""; while (!stack.isEmpty()) { result += stack.pop(); } return result; } public static void main(String[] args) throws Exception { String binary = SequenceStack.DexStringToBinary(67); System.out.println(binary); }}