Java Pilacoda polimorfa La Pila in Java 1

  • Slides: 25
Download presentation
Java Pila/coda polimorfa

Java Pila/coda polimorfa

La Pila in Java - 1 package strutture; public abstract class Stack { protected

La Pila in Java - 1 package strutture; public abstract class Stack { protected int size; protected int default. Growth. Size=5; protected int marker; protected Object contenuto[]; protected final int initial. Size=3; public Stack() { size=initial. Size; marker=0; contenuto=new Object[size]; }

La Pila in Java - 2 Abilita lo static binding public final void inserisci(Object

La Pila in Java - 2 Abilita lo static binding public final void inserisci(Object k) { if (marker==size) {cresci(default. Growth. Size); } contenuto[marker]=k; marker++; } public abstract Object estrai() ;

La Pila in Java - 3 private void cresci(int dim){ Object temp[ ]=new Object[size];

La Pila in Java - 3 private void cresci(int dim){ Object temp[ ]=new Object[size]; for (int k=0; k<size; k++) temp[k]=contenuto[k]; contenuto=new Object[size+default. Growth. Size]; for (int k=0; k<size; k++) contenuto[k]=temp[k]; size+=default. Growth. Size; }

La Pila in Java - 4 package strutture; public class Pila extends Stack {

La Pila in Java - 4 package strutture; public class Pila extends Stack { public Object estrai() { assert(marker>0): "Estrazione da Pila vuota"; return contenuto[--marker]; } }

La Coda in Java - 5 package strutture; public class Coda extends Stack {

La Coda in Java - 5 package strutture; public class Coda extends Stack { Object estrai() { assert(marker>0): "Estrazione da Coda vuota"; Object retval=contenuto[0]; for (int k=1; k<marker; k++ ) contenuto[k-1]=contenuto[k]; marker--; return retval; } }

La Pila in Java – 6 a public static void main(String args[]) { int

La Pila in Java – 6 a public static void main(String args[]) { int dim=10; Stack s=new Pila(); // s= new Coda(); for (int k=0; k<dim; k++){ Integer o=new Integer(k); s. inserisci(o); } for (int k=0; k<3*dim; k++) { Integer i = s. estrai(); int w=i. int. Value(); System. out. println(w); } }

La Pila in Java – 6 b public static void main(String args[]) { int

La Pila in Java – 6 b public static void main(String args[]) { int dim=10; Pila s=new Pila(); for (int k=0; k<dim; k++){ Integer o=new Integer(k); s. inserisci(o); } for (int k=0; k<3*dim; k++) { ERRORE! Integer i = s. estrai(); int w=i. int. Value(); Non posso System. out. println(w); mettere un } Object in un } Integer!

La Pila in Java – 6 c public static void main(String args[]) { int

La Pila in Java – 6 c public static void main(String args[]) { int dim=10; Pila s=new Pila(); for (int k=0; k<dim; k++){ Integer o=new Integer(k); s. inserisci(o); } for (int k=0; k<3*dim; k++) { Integer i = (Integer)s. estrai(); int w=i. int. Value(); System. out. println(w); } }

La Pila in Java – 7 a public static void main(String args[]) { int

La Pila in Java – 7 a public static void main(String args[]) { int dim=10; Pila s=new Pila(); //INSERIMENTO for (int k=0; k<dim; k++){ Object o; if (Math. random()<0. 5) o=new Integer(k); else o=new Float(k*Math. PI); s. inserisci(o); }

La Pila in Java – 7 b // ESTRAZIONE for (int k=0; k<dim; k++)

La Pila in Java – 7 b // ESTRAZIONE for (int k=0; k<dim; k++) { Object o = s. estrai(); if (o instanceof Integer) { Integer i = (Integer) o; int w = i. int. Value(); System. out. println("an int: "+w); } else if (o instanceof Float) { Float i = (Float) o; float w = i. float. Value(); System. out. println("a float: "+w); } else System. out. println("Unknown class!"); } }

La Pila in Java – 7 c OUTPUT: a float: 28. 274334 an int:

La Pila in Java – 7 c OUTPUT: a float: 28. 274334 an int: 8 an int: 7 a float: 18. 849556 an int: 5 an int: 4 a float: 9. 424778 a float: 6. 2831855 a float: 3. 1415927 a float: 0. 0

Java Generics

Java Generics

Definizione A generic type is a reference type that has one or more type

Definizione A generic type is a reference type that has one or more type parameters. In the definition of the generic type, the type parameter section follows the type name. It is a comma separated list of identifiers and is delimited by angle brackets. class Pair<X, Y> { private X first; private Y second; public Pair(X a 1, Y a 2) { first = a 1; second = a 2; } public X get. First() { return first; } public Y get. Second() { return second; } public void set. First(X arg) { first = arg; } public void set. Second(Y arg) { second = arg; } }

Definizione - continua The class Pair has two type parameters X and Y. They

Definizione - continua The class Pair has two type parameters X and Y. They are replaced by type arguments when the generic type Pair is instantiated. For instance, in the declaration Pair<String, Date> the type parameter X is replaced by the type argument String and Y is replaced by Date. The scope of the identifiers X and Y is the entire definition of the class. In this scope the two type parameters X and Y are used like they were types (with some restrictions).

Esempio public void print. Pair( Pair<String, Long> pair) { System. out. println("("+pair. get. First()+",

Esempio public void print. Pair( Pair<String, Long> pair) { System. out. println("("+pair. get. First()+", “ +pair. get. Second()+")"); } Pair<String, Long> limit = new Pair<String, Long> ("maximum", 1024 L); print. Pair(limit);

Wildcard instantiation public void print. Pair( Pair<? , ? > pair) { System. out.

Wildcard instantiation public void print. Pair( Pair<? , ? > pair) { System. out. println("("+pair. get. First()+", “ +pair. get. Second()+")"); } Pair<? , ? > limit = new Pair<String, Long> ("maximum", 1024 L); print. Pair(limit);

Generics are not usable… wfor creation of arrays win an instanceof expression

Generics are not usable… wfor creation of arrays win an instanceof expression

Referenze su generics: Il meglio: http: //www. angelikalanger. com/Generics. FAQ /Java. Generics. FAQ. html

Referenze su generics: Il meglio: http: //www. angelikalanger. com/Generics. FAQ /Java. Generics. FAQ. html

Uso di Generics nelle API di Java Here is a simple example taken from

Uso di Generics nelle API di Java Here is a simple example taken from the existing Collections tutorial: // Removes 4 -letter words from c. Elements must be strings static void expurgate(Collection c) { In Java 5 for (Iterator i = c. iterator(); i. has. Next(); ) if (((String) i. next()). length() == 4) molte classi i. remove(); sono state } riscritte Here is the same example modified to use generics: // Removes the 4 -letter words from c static void expurgate(Collection<String> c) { for (Iterator<String> i = c. iterator(); i. has. Next(); ) if (i. next(). length() == 4) i. remove(); } usando i generics

Generic Pila public class Pila <T> { … public Pila () {… } private

Generic Pila public class Pila <T> { … public Pila () {… } private void cresci(int dim) {…} public final void inserisci(T k) { if (marker == size) { cresci(default. Growth. Size); } contenuto[marker] = k; marker++; } public T estrai() { assert(marker > 0): "Estrazione da Pila return (T) contenuto[--marker]; } vuota";

Generic Pila - compilazione javac Pila. java –source 1. 5

Generic Pila - compilazione javac Pila. java –source 1. 5

Generic Pila public static void main(String args[]) { int dim = 10; Pila<Integer> s

Generic Pila public static void main(String args[]) { int dim = 10; Pila<Integer> s = new Pila<Integer>(); for (int k = 0; k < dim; k++) { s. inserisci(new Integer(k)); } for (int k = 0; k < 3 * dim; k++) { Integer w = s. estrai(); // Integer w = (Integer) s. estrai(); System. out. println(w); } } }

Generic Pila public class Pila <T> { … public Pila () {… } private

Generic Pila public class Pila <T> { … public Pila () {… } private void cresci(int dim) {…} public final void inserisci(T k) { if (marker == size) { cresci(default. Growth. Size); } contenuto[marker] = k; Note: Pila. java marker++; uses unchecked or } unsafe operations. public T estrai() { assert(marker > 0): "Estrazione da Pila return (T) contenuto[--marker]; } vuota";

Generic Pila public static void main(String args[]) { int dim = 10; Pila<Integer> s

Generic Pila public static void main(String args[]) { int dim = 10; Pila<Integer> s = new Pila<Integer>(); for (int k = 0; k < dim; k++) { s. inserisci(new String("pippo"))); } for (int k = 0; k < 3 * dim; k++) { Integer w = s. estrai(); // Integer w = (Integer) s. estrai(); System. out. println(w); } Pila. java: 43: inserisci(java. lang. Integer) } in Pila<java. lang. Integer> cannot be } applied to (java. lang. String) s. inserisci(new String("pippo")); ^