import java.io.IOException; class StackFehler extends Exception{ public StackFehler(){super();} public StackFehler(String s){super(s);} } class Stack{ private Object[] inhalt; private int topIndex; private int maxIndex; Stack(int sz){ inhalt = new Object[sz]; topIndex = 0; maxIndex = sz-1; } boolean istLeer (){ return topIndex <= 0;} boolean istVoll (){ return topIndex > maxIndex;} void push (Object e) throws StackFehler{ if (inhalt == null) throw new StackFehler("StackFehler: Kein Stack gefunden !"); if (istVoll()) throw new StackFehler("StackFehler: Stack Überlauf !"); inhalt[topIndex] = e; topIndex++; } void pop () throws StackFehler { if (inhalt == null) throw new StackFehler("StackFehler: Kein Stack gefunden !"); if (istLeer()) throw new StackFehler("StackFehler: Zugriff auf leeren Stack !"); topIndex--; } Object top () throws StackFehler { if (inhalt == null) throw new StackFehler("StackFehler: Kein Stack gefunden !"); if (istLeer()) throw new StackFehler("StackFehler: Zugriff auf leeren Stack !"); return inhalt[topIndex-1]; } Object popTop () throws StackFehler { Object e = top(); pop(); return e; } } public class ArStack { public static void main(String args[]) { System.out.println("Array Stack Tester"); System.out.println(""); String beispiel = "BEISPIEL"; int l = beispiel.length(); Stack meinStack = new Stack(l); try { for (int i = 0; i < l; i++){ char c = beispiel.charAt(i); System.out.print(c); //Character e = new Character(c);// nicht mehr nötig ab jdk1.5 //meinStack.push(e);// nicht mehr nötig ab jdk1.5 meinStack.push(c); } System.out.println(); } catch(StackFehler s){System.out.println(s);} try { for (int i = 0; i < l; i++){ Object e = meinStack.popTop(); System.out.print(e); } System.out.println(); } catch(StackFehler s){System.out.println(s);} } }