1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.util.EmptyStackException;
public class Pile {
private Object[] contenu;
private int taille = 0;
public Pile(int capacite)
{
contenu = new Object[capacite];
}
private void assurance () {
if (contenu.length == taille) {
Object ancien = contenu;
contenu = new Object[4*taille + 1] ;
System.arraycopy(ancien, 0, contenu, 0, taille);
}
}
public void add(Object object) {
assurance();
contenu[taille++] = object;
}
public Object sommet() {
if (taille == 0) {
throw new EmptyStackException();
}
return contenu[--taille];
}
} |
Partager