Java Reference
In-Depth Information
1
package weiss.util;
2
3
/**
4
* Stack class. Unlike java.util.Stack, this is not extended from
5
* Vector. This is the minimum respectable set of operations.
6
*/
7
public class Stack<AnyType> implements java.io.Serializable
8
{
9
public Stack( )
10
{
11
items = new ArrayList<AnyType>( );
12
}
13
14
public AnyType push( AnyType x )
15
{
16
items.add( x );
17
return x;
18
}
19
20
public AnyType pop( )
21
{
22
if( isEmpty( ) )
23
throw new EmptyStackException( );
24
return items.remove( items.size( ) - 1 );
25
}
26
27
public AnyType peek( )
28
{
29
if( isEmpty( ) )
30
throw new EmptyStackException( );
31
return items.get( items.size( ) - 1 );
32
}
33
34
public boolean isEmpty( )
35
{
36
return size( ) == 0;
37
}
38
39
public int size( )
40
{
41
return items.size( );
42
}
43
44
public void clear( )
45
{
46
items.clear( );
47
}
48
49
private ArrayList<AnyType> items;
50
}
figure 16.28
A simplified
Collections-style
Stack
class, based on
the
ArrayList
class
Search WWH ::
Custom Search