Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1.  
  2. public class ConjuntoTabla implements Conjunto {
  3.     //Descripció general: Implementación en forma de tabla de enteros
  4.    
  5.     private int tablaInt[]; //Tabla de enteros ordenada de forma creciente
  6.    
  7.     public void añadir(int n)
  8.     //Pre: --
  9.     //Post: Se ha añadido n al conjunto ordenadamente
  10.     {
  11.         if (!this.pertanece(n)) { //No puede contener elementos duplicados
  12.             int i = this.nelem();
  13.             while (i > 0 && n < this.tablaInt[i-1]) {
  14.                 this.taulaInt[i] = this.tablaInt[i-1];
  15.                 i--;
  16.             }
  17.             this.tablaInt[i] = n;
  18.         }
  19.     }
  20.    
  21.    
  22.     public Conjunto union(Conjunto c) {
  23.         Conjunto union = new ConjuntoTabla();
  24.         for (int i=0; i < this.nelem(); i++) {
  25.            [B] union.tablaInt[i] = this.tablaInt[i];[/B] //Hace una copia del array actual para evitar modificarlo
  26.         }
  27.         for (int i=0; i < c.nelem(); i++) {
  28.              [B]union.añadir(c.tablaInt[i]);[/B] //Ya comprueba que no haya duplicados
  29.         }
  30.         return union;
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement