Java
Pila/coda polimorfa
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];
}
La Pila in Java - 2
Abilita lo
static
binding
public final void inserisci(Object k) {
if (marker==size)
{cresci(defaultGrowthSize);}
contenuto[marker]=k;
marker++;
}
public abstract Object estrai() ;
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;
}
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 {
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 – 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);
}
}
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++) {
ERRORE!
Integer i = s.estrai();
int w=i.intValue();
Non posso
System.out.println(w);
mettere un
}
Object in un
}
Integer!
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);
}
}
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);
}
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!");
}
}
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
Java
Generics
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 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; }
}
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 printPair( Pair<String,Long> pair) {
System.out.println("("+pair.getFirst()+",“
+pair.getSecond()+")");
}
Pair<String,Long> limit =
new Pair<String,Long> ("maximum",1024L);
printPair(limit);
Wildcard instantiation
public void printPair( Pair<?,?> pair) {
System.out.println("("+pair.getFirst()+",“
+pair.getSecond()+")");
}
Pair<?,?> limit =
new Pair<String,Long> ("maximum",1024L);
printPair(limit);
Generics are not usable…
for creation of arrays
in an instanceof expression
Referenze su generics:
Il meglio:
http://www.angelikalanger.com/GenericsFAQ
/JavaGenericsFAQ.html
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();
}
In Java 5
molte classi
sono state
riscritte
usando i
generics
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.hasNext(); )
if (i.next().length() == 4)
i.remove();
}
Generic Pila
public class Pila <T> {
…
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
return (T) contenuto[--marker];
}
vuota";
Generic Pila - compilazione
javac Pila.java –source 1.5
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 void cresci(int dim) {…}
public final void inserisci(T k) {
if (marker == size) {
cresci(defaultGrowthSize);
}
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 = 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"));
^
Scarica

Document