Download presentation
Presentation is loading. Please wait.
Published byMegan McCann Modified over 11 years ago
1
Java Pila/coda polimorfa
2
La Pila in Java - 1 package strutture; public abstract class Stack { protected int size; protected int defaultGrowthSize=5; protected int marker; protected Object contenuto[]; protected final int initialSize=3; public Stack() { size=initialSize; marker=0; contenuto=new Object[size]; }
3
La Pila in Java - 2 public final void inserisci(Object k) { if (marker==size) {cresci(defaultGrowthSize);} contenuto[marker]=k; marker++; } public abstract Object estrai() ; Abilita lo static binding
4
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+defaultGrowthSize]; for (int k=0;k<size;k++) contenuto[k]=temp[k]; size+=defaultGrowthSize; }
5
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]; }
6
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; }
7
La Pila in Java – 6a 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.intValue(); System.out.println(w); }
8
La Pila in Java – 6b 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 = s.estrai(); int w=i.intValue(); System.out.println(w); } ERRORE! Non posso mettere un Object in un Integer!
9
La Pila in Java – 6c 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.intValue(); System.out.println(w); }
10
La Pila in Java – 7a 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); }
11
La Pila in Java – 7b // ESTRAZIONE for (int k=0;k<dim;k++) { Object o = s.estrai(); if (o instanceof Integer) { Integer i = (Integer) o; int w = i.intValue(); System.out.println("an int:"+w); } else if (o instanceof Float) { Float i = (Float) o; float w = i.floatValue(); System.out.println("a float:"+w); } else System.out.println("Unknown class!"); }
12
La Pila in Java – 7c 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
13
Java Generics
14
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 { private X first; private Y second; public Pair(X a1, Y a2) { first = a1; second = a2; } public X getFirst() { return first; } public Y getSecond() { return second; } public void setFirst(X arg) { first = arg; } public void setSecond(Y arg) { second = arg; } }
15
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 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).
16
Esempio public void printPair( Pair pair) { System.out.println("("+pair.getFirst()+", +pair.getSecond()+")"); } Pair limit = new Pair ("maximum",1024L); printPair(limit);
17
Wildcard instantiation public void printPair( Pair pair) { System.out.println("("+pair.getFirst()+", +pair.getSecond()+")"); } Pair limit = new Pair ("maximum",1024L); printPair(limit);
18
Generics are not usable… for creation of arrays in an instanceof expression
19
Referenze su generics: Il meglio: http://www.angelikalanger.com/GenericsFAQ /JavaGenericsFAQ.html
20
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) { for (Iterator i = c.iterator(); i.hasNext(); ) if (((String) i.next()).length() == 4) i.remove(); } Here is the same example modified to use generics: // Removes the 4-letter words from c static void expurgate(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) if (i.next().length() == 4) i.remove(); } In Java 5 molte classi sono state riscritte usando i generics
21
Generic Pila public class Pila { … public Pila () {… } private void cresci(int dim) {…} public final void inserisci(T k) { if (marker == size) { cresci(defaultGrowthSize); } contenuto[marker] = k; marker++; } public T estrai() { assert(marker > 0):" Estrazione da Pila vuota "; return (T) contenuto[--marker]; }
22
Generic Pila - compilazione javac Pila.java –source 1.5
23
Generic Pila public static void main(String args[]) { int dim = 10; Pila s = new Pila (); 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); }
24
Generic Pila public class Pila { … public Pila () {… } private void cresci(int dim) {…} public final void inserisci(T k) { if (marker == size) { cresci(defaultGrowthSize); } contenuto[marker] = k; marker++; } public T estrai() { assert(marker > 0):" Estrazione da Pila vuota "; return (T) contenuto[--marker]; } Note: Pila.java uses unchecked or unsafe operations.
25
Generic Pila public static void main(String args[]) { int dim = 10; Pila s = new Pila (); 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 cannot be applied to (java.lang.String) s.inserisci(new String( " pippo")); ^
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.