













Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!














public class IntList { private final static int _INIT_SIZE = 2; private Integer[] elem; private int last; public IntList() { last = -1; elem = new Integer[ _INIT_SIZE ]; } public void addElem(Integer e) { if (last == elem.length - 1) elem = Arrays. copyOf (elem, elem.length * 2); elem[++last] = e; } public Integer getElem( int index) { if (index > last) return null ; else return elem[index]; } }
public class IntList { private final static int _INIT_SIZE = 2; private Integer[] elem; private int last; public IntList() { last = -1; elem = new Integer[ _INIT_SIZE ]; } public void addElem(Integer e) { if (last == elem.length - 1) elem = Arrays. copyOf (elem, elem.length * 2); elem[++last] = e; } public Integer getElem( int index) { if (index > last) return null ; else return elem[index]; } } //* Main** IntList x = new IntList(); x.addElem(12); x.addElem(244); x.addElem(31); System. out .println(x.getElem(0)); System. out .println(x.getElem(1)); System. out .println(x.getElem(2));
public class StringList { private final static int _INIT_SIZE = 2; private String [] elem; private int last; public StringList() { last = -1; elem = new String [ _INIT_SIZE ]; } public void addElem( String e) { if (last == elem.length - 1) elem = Arrays. copyOf (elem, elem.length * 2); elem[++last] = e; } public String getElem( int index) { if (index > last) return null ; else return elem[index]; } }
public class StringList { private final static int _INIT_SIZE = 2; private String[] elem; private int last; public StringList() { last = -1; elem = new String[ _INIT_SIZE ]; } public void addElem(String e) { if (last == elem.length - 1) elem = Arrays. copyOf (elem, elem.length * 2); elem[++last] = e; } public String getElem( int index) { if (index > last) return null ; else return elem[index]; } } //* Main StringList** x = new StringList (); x.addElem(“Hola”); x.addElem(“Hey”); x.addElem(“bla”); System. out .println(x.getElem(0)); System. out .println(x.getElem(1)); System. out .println(x.getElem(2));
Integer z = (Integer) x.getElem(0);
Integer z = (Integer) x.getElem(0);
public class GenericList { private final static int INIT_SIZE = 2; private T[] elem; private int last; public GenericList() { this .last = -1; this .elem = (T[]) new Object[ INIT_SIZE ]; } public void addElem(T e) { if ( this .last == this .elem.length-1) elem = Arrays. copyOf (elem, elem.length * 2); this .elem[++last] = e; } public T getElem( int index) { if (index >= this .last) return null ; else return this .elem[index]; } }
public class GenericList< T extends Object> { private final static int INIT_SIZE = 2; private T [] elem; private int last; public GenericList() { this .last = -1; this .elem = ( T []) new Object[ INIT_SIZE ]; } public void addElem( T e) { if ( this .last == this .elem.length-1) elem = Arrays. copyOf (elem, elem.length * 2); this .elem[++last] = e; } public T getElem( int index) { if (index >= this .last) return null ; else return this .elem[index]; } } T es un parámetro de tipo
public class GenericList< T extends Object> { private final static int INIT_SIZE = 2; private T [] elem; private int last; public GenericList() { this .last = -1; this .elem = ( T []) new Object[ INIT_SIZE ]; } public void addElem( T e) { if ( this .last == this .elem.length-1) elem = Arrays. copyOf (elem, elem.length * 2); this .elem[++last] = e; } public T getElem( int index) { if (index >= this .last) return null ; else return this .elem[index]; } } T es un parámetro de tipo Object es la cota superior de T. Si es Object , no hace falta escribir “extends Object”
public static void main(String[] args) { GenericList x = new GenericList(); x.addElem(100); // MAL x.addElem("aaa"); String a1 = x.getElem(0); Integer a2 = x.getElem(0); // MAL GenericList y = new GenericList(); y.addElem(100); }
public class GenericList { private final static int INIT_SIZE = 2; private T[] elem; private int last; public GenericList() { this .last = -1; this .elem = ((T[]) new Object[ INIT_SIZE ]); } public void addElem(T e) { if ( this .last == this .elem.length-1) this .elem = Arrays. copyOf ( this .elem, this .elem.length * 2); this .elem[++last] = e; } public T getElem( int index) { if (index >= this .last) return null ; else return this .elem[index]; } }
public class GenericList { private final static int INIT_SIZE = 2; private T[] elem; private int last; public GenericList() { this .last = -1; this .elem = ((T[]) new Object[ INIT_SIZE ]); } public void addElem(T e) { if ( this .last == this .elem.length-1) this .elem = Arrays. copyOf ( this .elem, this .elem.length * 2); this .elem[++last] = e; } public T getElem( int index) { if (index >= this .last) return null ; else return this .elem[index]; } } public class GenericList { private final static int INIT_SIZE = 2; private Object [] elem; private int last; public GenericList() { this .last = -1; this .elem = new Object[ INIT_SIZE ]; } public void addElem( Object e) { if ( this .last == this .elem.length-1) this .elem = Arrays. copyOf ( this .elem, this .elem.length * 2); this .elem[++ this .last] = e; } public Object getElem( int index) { if (index >= last) return null ; else return this .elem[index]; } }
public class GenericList { private final static int INIT_SIZE = 2; private T[] elem; private int last; public GenericList() { this .last = -1; this .elem = ((T[]) new Object[ INIT_SIZE ]); } public void addElem(T e) { if ( this .last == this .elem.length-1) this .elem = Arrays. copyOf ( this .elem, this .elem.length * 2); this .elem[++last] = e; } public T getElem( int index) { if (index >= this .last) return null ; else return this .elem[index]; } } public class GenericList { private final static int INIT_SIZE = 2; private Object[] elem; private int last; public GenericList() { this .last = 0; this .elem = new Object[ INIT_SIZE ]; } public void addElem(Object e) { if ( this .last == this .elem.length-1) this .elem = Arrays. copyOf ( this .elem, this .elem.length * 2); this .elem[++ this .last] = e; } public Object getElem( int index) { if (index >= last) return null ; else return this .elem[index]; } } GenericList y = new MyList(); y.addElem(100); Integer z = y.getElem(0); GenericList y = new GenericList(); y.addElem(100); Integer z = (Integer) y.getElem(0);
public class Pair **** { private A first; private B second; public Pair(A first, B second) { this .first = first; this .second = second; } public A getFirst() { return first; } public B getSecond() { return second; } public String toString() { return "(" + first + "," + second + ")"; } }
public class Pair **** { private A first; private B second; public Pair(A first, B second) { this .first = first; this .second = second; } public A getFirst() { return first; } public B getSecond() { return second; } public String toString() { return "(" + first + "," + second + ")"; } } Pair x= new Pair(1,"hola"); Pair y= new Pair(3,"adios"); System. out .println(x); System. out .println(y);
public class Pair>** { private A first; private B second; public Pair(A first, B second) {...} public A getFirst() {...} public B getSecond() {...} public String toString() {...} } static public void main(String[] args) { GenericList l1 = new GenericList(); GenericList l2 = new GenericList(); l1.addElem(1); l2.addElem(2.0); Pair> x = new Pair>(1,l1); Pair> y = new Pair>(3.0,l2); System. out .println(x); System. out .println(y); }
abstract public class ListI { protected T[] elem; protected int last; protected final static int INIT_SIZE = 2; public ListI() { last = -1; elem = (T[]) new Object[ INIT_SIZE ]; } public abstract void addElem(T e); public T getElem( int index) { if (index > last) return null ; else return elem[index]; } public int size(){ return last + 1; } }
abstract public class ListI { protected T[] elem; protected int last; protected final static int INIT_SIZE = 2; public ListI() { last = -1; elem = (T[]) new Object[ INIT_SIZE ]; } public abstract void addElem(T e); public T getElem( int index) { if (index > last) return null ; else return elem[index]; } public int size(){ return last + 1; } } // redimensiona el tamaño public class MyList1 extends ListI { @Override public void addElem(T e) { if (last == elem.length - 1) elem = Arrays. copyOf (elem, elem.length * 2); elem[++last] = e; } }
abstract public class ListI { protected T[] elem; protected int last; protected final static int INIT_SIZE = 2; public ListI() { last = -1; elem = (T[]) new Object[ INIT_SIZE ]; } public abstract void addElem(T e); public T getElem( int index) { if (index > last) return null ; else return elem[index]; } public int size(){ return last + 1; } } // redimensiona el tamaño public class MyList1 extends ListI { @Override public void addElem(T e) { if (last == elem.length - 1) elem = Arrays. copyOf (elem, elem.length * 2); elem[++last] = e; } } // sino cabe el elemento no se mete public class MyList2 extends ListI { @Override public void addElem(T e) { if (last < elem.length - 1) elem[++last] = e; } }
public class Utils { public static boolean compare(Pair p1, Pair p2) { return p1.getFirst().equals(p2.getFirst()) && p2.getSecond().equals(p2.getSecond()); } }
public class Utils { // hay que indicar los parámetros genéricos public static boolean compare(Pair p1, Pair p2) { return p1.getFirst().equals(p2.getFirst()) && p2.getSecond().equals(p2.getSecond()); } }
public class Utils { // hay que indicar los parámetros genéricos public static boolean compare(Pair p1, Pair p2) { return p1.getFirst().equals(p2.getFirst()) && p2.getSecond().equals(p2.getSecond()); } } Pair x = new Pair(1,"hola"); Pair y = new Pair(1,"hola"); if (Utils. compare (x, y)) System. out .println("iguales"); else System. out .println("distintos");
public class Utils { // hay que indicar los parámetros genéricos public static boolean compare(Pair p1, Pair p2) { return p1.getFirst().equals(p2.getFirst()) && p2.getSecond().equals(p2.getSecond()); } } Pair x = new Pair(1,"hola"); Pair y = new Pair(1,"hola"); if (Utils. compare (x, y)) System. out .println("iguales"); else System. out .println("distintos"); No hace falta escribir los tipos en la llamada. el compilador infiere que son usando los tipos de x e y.
public class Utils { public static void print( MyList t ){ for ( int i=0; i< t.size(); i++) System. out .println(t.getElem(i)); } }
public static void print(MyList t){ for ( int i=0; i< t.size(); i++) System. out .println(i); }
public static void print(MyList t){ for ( int i=0; i< t.size(); i++) System. out .println(i); }
public static void print(MyList t){ for ( int i=0; i< t.size(); i++) System. out .println(i); }
public static void copy(GenericList l1, GenericList l2){ ... } Se obliga a que ambos parámetros sean exactamente del mismo tipo. No se puede hacer: GenericList l1 = new GenericList(); GenericList l2 = new GenericList(); copy(l1,l2) public static void copy(GenericList l1, GenericList l2){ ... } Ambos parámetros deben extender a Number, pero pueden ser distintos. Por lo Tanto es correcto copy(l1,l2)
public static void print(GenericList l){ ... }
public static void print(GenericList l){ ... } ESTÁ PROHIBIDO – No compila –
public static void print(GenericList l){ ... } ESTÁ PROHIBIDO – No compila – public static void print(GenericList l){ ... }
public static void print(GenericList l){ ... } ESTÁ PROHIBIDO – No compila – public static void print(GenericList l){ ... } CORRECTO !!!
https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html
public class RationalArithmetics extends BinaryArithmetics { public RationalArithmetics(Rational r1, Rational r2) { super (r1,r2); } @Override public Rational add() { int n = ( this .x.getNum() * this .y.getDen()) + ( this .x.getDen()* this .y.getNum()); int d = ( this .x.getDen() * this .y.getDen()); return new Rational(n,d); } @Override public Rational sub() { int n = ( this .x.getNum() * this .y.getDen()) - ( this .x.getDen()* this .y.getNum()); int d = ( this .x.getDen() * this .y.getDen()); return new Rational(n,d); } @Override public Rational div() { int n = this .x.getNum() * this .y.getDen(); int d = this .x.getDen() * this .y.getNum(); return new Rational(n,d); } @Override public Rational mult() { ... } }
public static void main(String[] args) { Rational r1 = new Rational(2,3); Rational r2 = new Rational(7,8); RationalArithmetics r = new RationalArithmetics(r1,r2); System. out .println(r.add()); DoubleArithmetics d1 = new DoubleArithmetics(2.3,1.2); System. out .println(d1.div()); }
abstract public class Comparable { public abstract boolean less(T e); public abstract boolean greater(T e); public boolean equal(T e){ return (! this .less(e) &&! this .greater(e)); } public boolean lessOrEquals(T e){ return ( this .less(e) || this .equals(e)); } public boolean graterOrEqualsThan(T e){ return ( this .greater(e) || this .equals(e)); } }
public class Person extends Comparable { private String name; private int id; Person(String name, int id) { this .name = name; this .id = id; } public String toString() { … } @Override public boolean less(Person e) { return this .id < e.id; } @Override public boolean greater(Person e) { return this .id > e.id; } }
public class Person extends Comparable { private String name; private int id; Person(String name, int id) { this .name = name; this .id = id; } public String toString() { … } @Override public boolean less(Person e) { return this .id < e.id; } @Override public boolean greater(Person e) { return this .id > e.id; } }
public class Account extends Comparable{ private int numCuenta; private int saldo; public Account( int nc, int s) { this .numCuenta = nc; this .saldo = s; } @Override public boolean less(Account e) { return this .numCuenta < e.numCuenta; } @Override public boolean greater(Account e) { return this .numCuenta > e.numCuenta; } public String toString() { return "Numero Cuenta: " + this .numCuenta + " Saldo: " + this .saldo; } }
public class Account extends Comparable{ private int numCuenta; private int saldo; public Account( int nc, int s) { this .numCuenta = nc; this .saldo = s; } @Override public boolean less(Account e) { return this .numCuenta < e.numCuenta; } @Override public boolean greater(Account e) { return this .numCuenta > e.numCuenta; } public String toString() { return "Numero Cuenta: " + this .numCuenta + " Saldo: " + this .saldo; } }
public class SortExample { public static void sort( int[] a ) { for ( int i = 1; i < a.length; i++) { int x = a[i]; int j = i - 1; while (j >= 0 && a[j] > x) { a[j + 1] = a[j]; j--; } a[j + 1] = x; } } public static < T extends Comparable > void sort(T[] a) { for ( int i = 1; i < a.length; i++) { T x = a[i]; int j = i - 1; while (j >= 0 && a[j]. greater (x)) { a[j + 1] = a[j]; j--; } a[j + 1] = x; } } }