gonzalob

Untitled

May 14th, 2020
1,784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package clases;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import interfaces.Interfaz5_InterfazColeccion;
  6.  
  7. public class Pila implements IColeccion  {
  8.  
  9.     private ArrayList<Integer> pila;
  10.     private int contadorObjetos;
  11.  
  12.     public Pila () {
  13.  
  14.         pila = new ArrayList<Integer>();
  15.         contadorObjetos = 0;
  16.     }
  17.  
  18.     @Override
  19.     public boolean estaVacia() {
  20.  
  21.         boolean rta = false;
  22.  
  23.         if (contadorObjetos == 0) {
  24.  
  25.             rta = true;
  26.         }
  27.         return rta;
  28.     }
  29.  
  30.     @Override
  31.     public Object extraer() {
  32.         Integer tope = null;
  33.         if (!estaVacia()) {
  34.  
  35.             tope = pila.set(contadorObjetos - 1, null); // me pisa el ultimo valor y me retorna el "tope" o null si
  36.             contadorObjetos--; // estaba vacĂ­a
  37.         }
  38.         return tope;
  39.     }
  40.  
  41.     @Override
  42.     public Object primero() {
  43.  
  44.         Integer tope = null;
  45.         if (!estaVacia()) {
  46.  
  47.             tope = pila.get(contadorObjetos - 1);
  48.         }
  49.  
  50.         return tope;
  51.     }
  52.  
  53.     @Override
  54.     public boolean agregar(Object obj) {
  55.  
  56.         boolean exito = false;
  57.         if (obj != null) {
  58.             if (obj instanceof Integer) {
  59.                 Integer aux = (Integer) obj;
  60.                 pila.add(aux);
  61.                 contadorObjetos++;
  62.                 exito = true;
  63.             }
  64.         }
  65.         return exito;
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment