java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.Vector<E>
java.util.Stack<E>
- All Implemented Interfaces:
- Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
- See Also:
- Top Examples, Source Code
public boolean empty()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1586]Use Iterator on Stack
By Anonymous on 2005/11/04 20:23:08 Rate
//You can also use Iterator on Stack
Stack path = new Stack ( ) ;
Iterator each = path.iterator ( ) ;
while ( each.hasNext ( ) )
System.out.println ( ( String ) each.next ( ) ) ;
public E peek()
- See Also:
- EmptyStackException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public E pop()
- See Also:
- EmptyStackException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1703]Object loitering in array-based collection
By just a geek on 2006/01/24 10:12:27 Rate
public class LeakyStack {
private Object [ ] elements = new Object [ MAX_ELEMENTS ] ;
private int size = 0;
public void push ( Object o ) { elements [ size++ ] = o; }
// In the pop ( ) method, after the top pointer is decremented,
// elements still maintains a reference to the object being
// popped off the stack. This means that a reference to that
// object is still reachable by the program even though the
// program never actually uses that reference again, which
// prevents that object from being garbage collected until
// that location is reused by a future push ( ) .
// A linked implementation of this algorithm would not have this
// problem; in a linked implementation, the lifetime of the link node
// ( and therefore the reference to the object being stored )
// would be automatically tied to the duration that the object
// is stored in the collection.
public Object pop ( ) {
if ( size == 0 )
throw new EmptyStackException ( ) ;
else {
Object result = elements [ --size ] ;
//null out the reference after popping it from the stack to
//fix the memory leak
elements [ size+1 ] = null;
return result;
}
}
}
public E push(E item)
- See Also:
Vector.addElement(E)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public Object push(Object item)
- See Also:
Vector.addElement(java.lang.Object)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public int search(Object o)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public Stack()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[48]Stack is such a poorly designed class
By Anonymous on 2004/11/06 00:46:24 Rate
import java.util.*;
public class StackTest {
public static void vectorShow ( Vector v ) {
System.out.println ( "PRINTING VECTOR CONTENTS" ) ;
for ( int i = 0, length = v.size ( ) ; i < length; i++ )
System.out.println ( v.get ( i ) ) ;
}
public static void main ( String [ ] args ) {
Stack myStack = new Stack ( ) ;
//Treat as a Stack
myStack.push ( "foo" ) ;
System.out.println ( myStack.pop ( ) ) ;
//Treat as a vector
myStack.add ( "1" ) ;
myStack.add ( "2" ) ;
myStack.add ( "3" ) ;
System.out.println ( myStack.get ( 2 ) ) ;
System.out.println ( myStack.contains ( "1" ) ) ;
myStack.insertElementAt ( "5", 1 ) ;
myStack.removeElementAt ( 2 ) ;
vectorShow ( myStack ) ;
}
}
[1295]_
By Anonymous on 2005/07/07 14:38:40 Rate
import java.util.*;
public class StackTest {
public static void main ( String arg [ ] ) throws IOException {
String name [ ] = new String [ 5 ] ;
String choice;
int x;
char chris
BufferedReader pangan = new BufferedReader ( new InputStreamReader ( System.in ) ) ;
Stack unstack = Stack ( ) ;
System.out.print ( "Peek\n" ) ;
System.out.print ( "Push\n" ) ;
System.out.print ( "Pop\n" ) ;
System.out.print ( "Enter Choice:\n" ) ;
choice = pangan.redLine ( ) ;
chris = choice.charAt ( 1 ) ;
switch ( ch )