001package clone.stack; 002public final class IntegerStack implements Cloneable { 003 private int[] buffer; 004 private int top; 005 006 public IntegerStack(int maxContents) { 007 buffer = new int[maxContents]; 008 top = -1; 009 } 010 public void push(int val) { 011 buffer[++top] = val; 012 } 013 public int pop() { 014 return buffer[top--]; 015 } 016 public Object clone() { 017 try { 018 IntegerStack result = (IntegerStack) super.clone(); 019 result.buffer = buffer.clone(); 020 return result; 021 } catch (CloneNotSupportedException e) { 022 // cannot happen 023 throw new RuntimeException(e); 024 } 025 } 026}