Java Reference
In-Depth Information
// Could check capacity and expand
stack
[
depth
++] =
obj
;
}
/* pop - return and remove the top element */
@Override
public
public
T
pop
() {
--
depth
;
T tmp
=
stack
[
depth
];
stack
[
depth
] =
null
null
;
return
return
tmp
;
}
/** peek - return the top element but don't remove it */
@Override
public
public
T
peek
() {
iif
(
depth
==
0
) {
return
return null
null
;
}
return
return
stack
[
depth
-
1
];
}
public
public
boolean
boolean
hasNext
() {
return
return
depth
>
0
;
}
public
public
boolean
boolean
hasRoom
() {
return
return
depth
<
stack
.
length
;
}
public
public
int
int
getStackDepth
() {
return
return
depth
;
}
}
The association of a particular type is done at the time the class is instantiated. For example,
to instantiate a
MyStack
specialized for holding
BankAccount
objects, you would need to
code only the following:
MyStack
<
BankAccount
>
theAccounts
=
new
new
MyStack
<>( );