import java.util.Vector;
public class StackTest extends Vector<Object> {
public void push(Object o) {
this.addElement(o);
}
public Object pop() {
Object o = this.peek();
int len = this.size();
if (o != null)
this.remove(len - 1);
return o;
}
public Object peek() {
int len = this.size();
if (len > 0) {
return elementAt(len - 1);
}
return null;
}
public int search(Object o) {
int i = this.lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
public boolean empty() {
return this.size() == 0;
}
public static void main(String[] args) {
StackTest s = new StackTest();
int len=10;
for(int i=0; i< len; i++){
s.push(new Integer(i));
}
System.out.println("Spitze(1): " + ((Integer) s.peek()).intValue());
for(int i=0; i<4; i++){
System.out.println("entnehme: " + s.pop());
}
System.out.println("Spitze(2): " + ((Integer) s.peek()).intValue());
System.out.println("3 an Position: " + s.search(new Integer(3)));
}
}
Die Ausgabe ergibt
public class StackTest extends Vector<Object> {
public void push(Object o) {
this.addElement(o);
}
public Object pop() {
Object o = this.peek();
int len = this.size();
if (o != null)
this.remove(len - 1);
return o;
}
public Object peek() {
int len = this.size();
if (len > 0) {
return elementAt(len - 1);
}
return null;
}
public int search(Object o) {
int i = this.lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
public boolean empty() {
return this.size() == 0;
}
public static void main(String[] args) {
StackTest s = new StackTest();
int len=10;
for(int i=0; i< len; i++){
s.push(new Integer(i));
}
System.out.println("Spitze(1): " + ((Integer) s.peek()).intValue());
for(int i=0; i<4; i++){
System.out.println("entnehme: " + s.pop());
}
System.out.println("Spitze(2): " + ((Integer) s.peek()).intValue());
System.out.println("3 an Position: " + s.search(new Integer(3)));
}
}
Spitze(1): 9 entnehme: 9 entnehme: 8 entnehme: 7 entnehme: 6 Spitze(2): 5 3 an Position: 3