Algorithmen und Datenstrukturen Wintersemester 2012/13 21. Vorlesung Interfaces und Generics Jan-Henrik Haunert Lehrstuhl für Informatik I
Übersicht Liste und InsertionSort für Punkte für Objekte beliebiger Klassen für Objekte von Klassen, die ein(e) Nutzer(in) festlegen kann
Übersicht Liste und InsertionSort für Punkte (Wiederholung vom 15.11.2012) für Objekte beliebiger Klassen für Objekte von Klassen, die ein(e) Nutzer(in) festlegen kann
InsertionSort für Punkte Ein Punkt in der Ebene: public class Punkt { public double x; public double y; UML-Diagramm: +x:double +y:double Punkt Voraussetzung fürs Sortieren: Definition einer Ordnung y p 3 p i < p j genau dann, wenn p 1 p i.x < p j.x oder ( p i.x = p j.x und p i.y < p j.y) 1 p 2 p 4 0 0 1 x = lexikographische Ordnung
Ein Punkt in der Ebene: InsertionSort für Punkte public class Punkt { public double x; public double y; public Punkt(double myx, double myy) { x = myx; y = myy; /* gibt Wert < 0 fuer (this < p), Wert > 0 fuer (this > p) und 0 fuer (this = p) zurueck */ public int compareto(punkt p) { if (this.x < p.x) return -1; if (this.x == p.x) { if (this.y < p.y) return -1; if (this.y == p.y) return 0; return 1; UML-Diagramm: +x:double +y:double Punkt +Punkt(double, double) +compareto(punkt):int
InsertionSort für Punkte public class Sortierverfahren { public static void insertionsort(int[] a) { for (int j = 1; j < a.length; j++) { int key = a[j]; int i = j - 1; while (i >= 0 && a[i] > key) { a[i + 1] = a[i]; i--; a[i + 1] = key;
InsertionSort für Punkte public class Sortierverfahren { public static void insertionsort(punkt[] a) { for (int j = 1; j < a.length; j++) { Punkt key = a[j]; int i = j - 1; while (i >= 0 && a[i].compareto(key) > 0) { a[i + 1] = a[i]; i--; a[i + 1] = key;
InsertionSort für Punkte public class Sortierverfahren { public static void main(string[] args) { Punkt[] a = new Punkt[4]; a[0] = new Punkt(1, 2); a[1] = new Punkt(1, 1); a[2] = new Punkt(3, 1); a[3] = new Punkt(4, 3); insertionsort(a); for (int i = 0; i < a.length; i++) System.out.println(a[i].x + " " + a[i].y); public static void insertionsort(punkt[] a) { //...
InsertionSort für Punkte public class Sortierverfahren { public static void main(string[] args) { Punkt[] a = new Punkt[4]; a[0] = new Punkt(1, 2); a[1] = new Punkt(1, 1); a[2] = new Punkt(3, 1); Ausgabe: a[3] = new Punkt(4, 3); insertionsort(a); 1.0 1.0 for (int i = 0; i < a.length; i++) System.out.println(a[i].x + " " + a[i].y); 1.0 2.0 3.0 1.0 public static void insertionsort(punkt[] a) { //... 4.0 3.0
InsertionSort für Punkte Problem: eigene Sortiermethode für jede Klasse notwendig public static void insertionsort(punkt[] a) { //...
Liste für Punkte x y head nil prev key next nil
Liste für Punkte x y head nil prev key next nil public class PunktListenElement { public Punkt key; public PunktListenElement prev; public PunktListenElement next; public PunktListenElement(Punkt k, PunktListenElement p) { key = k; next = p; prev = null;
Liste für Punkte public class PunktListe { public PunktListenElement head; public PunktListenElement insert(punkt k) { PunktListenElement x = new PunktListenElement(k, head); if (head!= null) { head.prev = x; head = x; return x; public class PunktListenElement { public Punkt key; public PunktListenElement prev; public PunktListenElement next; public PunktListenElement(Punkt k, PunktListenElement p) { key = k; next = p; prev = null;
Liste für Punkte public class PunktListenTest { public static void main(string[] args) { PunktListe l = new PunktListe(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); for (PunktListenElement p = l.head; p!= null; p = p.next) { System.out.println(p.key.x + " " + p.key.y);
Liste für Punkte public class PunktListenTest { public static void main(string[] args) { PunktListe l = new PunktListe(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); for (PunktListenElement p = l.head; p!= null; p = p.next) { System.out.println(p.key.x + " " + p.key.y); Ausgabe: 1.0 0.0 2.0 2.0 0.0 1.0
Liste für Punkte public class PunktListenTest { public static void main(string[] args) { PunktListe l = new PunktListe(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); for (PunktListenElement p = l.head; p!= null; p = p.next) { System.out.println(p.key.x + " " + p.key.y); Problem: eigene Liste für jede Klasse notwendig
Liste für Punkte public class PunktListenTest { public static void main(string[] args) { PunktListe l = new PunktListe(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); for (PunktListenElement p = l.head; p!= null; p = p.next) { System.out.println(p.key.x + " " + p.key.y); Problem: eigene Liste für jede Klasse notwendig Lösung: erstelle Liste für Objekte der Klasse java.lang.object
Liste für Objekte java.lang.object Punkt Jede Java-Klasse ist Unterklasse der Klasse java.lang.object Eine Liste, die Objekte vom Typ java.lang.object aufnehmen kann, kann also jedes Objekt aufnehmen.
Liste für Objekte public class PunktListe { public PunktListenElement head; public PunktListenElement insert(punkt k) { PunktListenElement x = new PunktListenElement(k, head); if (head!= null) { head.prev = x; head = x; return x; public class PunktListenElement { public Punkt key; public PunktListenElement prev; public PunktListenElement next; public PunktListenElement(Punkt k, PunktListenElement p) { key = k; next = p; prev = null;
Liste für Objekte public class Liste { public ListenElement head; public ListenElement insert(object k) { ListenElement x = new ListenElement(k, head); if (head!= null) { head.prev = x; head = x; return x; public class ListenElement { public Object key; public ListenElement prev; public ListenElement next; public ListenElement(Object k, ListenElement p) { key = k; next = p; prev = null;
Liste für Objekte public class PunktListenTest { public static void main(string[] args) { PunktListe l = new PunktListe(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); for (PunktListenElement p = l.head; p!= null; p = p.next) { System.out.println(p.key.x + " " + p.key.y);
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); for (ListenElement p = l.head; p!= null; p = p.next) { System.out.println(p.key.x + " " + p.key.y);
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); for (ListenElement p = l.head; p!= null; p = p.next) { System.out.println(p.key.x + " " + p.key.y); Compiler: "p.key.x cannot be resolved or is not a field"
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); for (ListenElement p = l.head; p!= null; p = p.next) { Punkt mypunkt = (Punkt) p.key; //explizite Typkonvertierung System.out.println(myPunkt.x + " " + mypunkt.y); Ausgabe: 1.0 0.0 2.0 2.0 0.0 1.0
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(3.2)); l.insert(new Integer(4)); Wir können nun auch Objekte anderer Klassen in die Liste einfügen
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(3.2)); l.insert(new Integer(4)); for (ListenElement p = l.head; p!= null; p = p.next) { Punkt mypunkt = (Punkt) p.key; //explizite Typkonvertierung System.out.println(myPunkt.x + " " + mypunkt.y);
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(3.2)); l.insert(new Integer(4)); for (ListenElement p = l.head; p!= null; p = p.next) { Punkt mypunkt = (Punkt) p.key; //explizite Typkonvertierung System.out.println(myPunkt.x + " " + mypunkt.y); Ausgabe: Exception in thread "main" java.lang.classcastexception: java.lang.integer cannot be cast to Punkt at ListenTest.main(ListenTest.java:12)
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(3.2)); l.insert(new Integer(4)); for (ListenElement p = l.head; p!= null; p = p.next) { if (p.key instanceof Punkt) { Punkt mypunkt = (Punkt) p.key; System.out.println(myPunkt.x + " " + mypunkt.y);
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(3.2)); l.insert(new Integer(4)); for (ListenElement p = l.head; p!= null; p = p.next) { if (p.key instanceof Punkt) { Punkt mypunkt = (Punkt) p.key; System.out.println(myPunkt.x + " " + mypunkt.y); instanceof überprüft, ob Objekt zu Klasse gehört
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(3.2)); l.insert(new Integer(4)); for (ListenElement p = l.head; p!= null; p = p.next) { if (p.key instanceof Punkt) { Punkt mypunkt = (Punkt) p.key; System.out.println(myPunkt.x + " " + mypunkt.y); Ausgabe: 1.0 0.0 2.0 2.0 0.0 1.0
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(3.2)); l.insert(new Integer(4)); for (ListenElement p = l.head; p!= null; p = p.next) { if (p.key instanceof Punkt) { Punkt mypunkt = (Punkt) p.key; System.out.println(myPunkt.x + " " + mypunkt.y); Ausgabe: 1.0 0.0 2.0 2.0 0.0 1.0...es werden nur die Punkte ausgegeben
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(3.2)); l.insert(new Integer(4)); for (ListenElement p = l.head; p!= null; p = p.next) { //gib Ergebnis von p.key.tostring() aus: System.out.println(p.key); Ausgabe: 4 3.2 Punkt@c3c749 Punkt@150bd4d Punkt@1bc4459
Liste für Objekte java.lang.object +tostring():string java.lang.double +tostring():string java.lang.integer +tostring():string Punkt for (ListenElement p = l.head; p!= null; p = p.next) { //gib Ergebnis von p.key.tostring() aus: System.out.println(p.key); Ausgabe: 4 tostring (Klasse Integer) 3.2 tostring (Klasse Double) Punkt@c3c749 tostring (Klasse Object) Punkt@150bd4d Punkt@1bc4459
Liste für Objekte java.lang.object +tostring():string java.lang.double +tostring():string java.lang.integer +tostring():string Punkt +tostring():string public class Punkt { public double x; public double y; public String tostring() { return "(" + x + ", " + y + ")"; //...
Liste für Objekte java.lang.object +tostring():string java.lang.double +tostring():string java.lang.integer +tostring():string Punkt +tostring():string public class Punkt { public double x; public double y; public String tostring() { return "(" + x + ", " + y + ")"; //... Methode tostring der Klasse Object wird überschrieben
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(3.2)); l.insert(new Integer(4)); for (ListenElement p = l.head; p!= null; p = p.next) { //gib Ergebnis von p.key.tostring() aus: System.out.println(p.key); Ausgabe: 4 3.2 Punkt@c3c749 Punkt@150bd4d Punkt@1bc4459 java.lang.object +tostring():string Punkt
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(3.2)); l.insert(new Integer(4)); for (ListenElement p = l.head; p!= null; p = p.next) { //gib Ergebnis von p.key.tostring() aus: System.out.println(p.key); Ausgabe: 4 3.2 (1.0, 0.0) (2.0, 2.0) (0.0, 1.0) java.lang.object +tostring():string Punkt +tostring():string
InsertionSort für Punkte Problem: eigene Sortiermethode für jede Klasse notwendig public static void insertionsort(punkt[] a) { //...
InsertionSort für Objekte public class Sortierverfahren { public static void insertionsort(punkt[] a) { for (int j = 1; j < a.length; j++) { Punkt key = a[j]; int i = j - 1; while (i >= 0 && a[i].compareto(key) > 0) { a[i + 1] = a[i]; i--; a[i + 1] = key;
InsertionSort für Objekte public class Sortierverfahren { public static void insertionsort(object[] a) { for (int j = 1; j < a.length; j++) { Object key = a[j]; int i = j - 1; while (i >= 0 && a[i].compareto(key) > 0) { a[i + 1] = a[i]; i--; a[i + 1] = key;
InsertionSort für Objekte public class Sortierverfahren { public static void insertionsort(object[] a) { for (int j = 1; j < a.length; j++) { Object key = a[j]; int i = j - 1; while (i >= 0 && a[i].compareto(key) > 0) { a[i + 1] = a[i]; i--; a[i + 1] = key; Compiler: "The method compareto(object) is undefined for the type Object"
InsertionSort für Objekte public class Sortierverfahren { public static void insertionsort(comparable[] a) { for (int j = 1; j < a.length; j++) { Comparable key = a[j]; int i = j - 1; while (i >= 0 && a[i].compareto(key) > 0) { a[i + 1] = a[i]; i--; a[i + 1] = key;
InsertionSort für Objekte public class Sortierverfahren { public static void insertionsort(comparable[] a) { for (int j = 1; j < a.length; j++) { Comparable key = a[j]; int i = j - 1; while (i >= 0 && a[i].compareto(key) > 0) { a[i + 1] = a[i]; i--; a[i + 1] = key; To Do: Definiere Klasse Comparable, so dass sichergestellt ist, dass Objekte von Unterklassen vergleichbar sind
InsertionSort für Objekte public class Comparable { public int compareto(object o) { //? To Do: Definiere Klasse Comparable, so dass sichergestellt ist, dass Objekte von Unterklassen vergleichbar sind
InsertionSort für Objekte public class Comparable { public int compareto(object o) { //? Es fällt uns schwer, eine allgemeine Definition zu finden.
InsertionSort für Objekte public class Comparable { public int compareto(object o) { //? Es fällt uns schwer, eine allgemeine Definition zu finden. Wir sollten daher verbieten, Instanzen unserer Klasse Comparable zu erzeugen.
InsertionSort für Objekte public abstract class Comparable { public abstract int compareto(object o); Es fällt uns schwer, eine allgemeine Definition zu finden. Wir sollten daher verbieten, Instanzen unserer Klasse Comparable zu erzeugen. Deshalb: abstract
InsertionSort für Objekte public abstract class Comparable { public abstract int compareto(object o); Die Klasse unserer Vergleichsobjekte hat evtl. bereits eine Oberklasse. Tier Elefant
InsertionSort für Objekte public abstract class Comparable { public abstract int compareto(object o); Die Klasse unserer Vergleichsobjekte hat evtl. bereits eine Oberklasse. Tier Comparable Elefant Mehrfachvererbung ist verboten!
InsertionSort für Objekte public interface Comparable { public int compareto(object o); Die Klasse unserer Vergleichsobjekte hat evtl. bereits eine Oberklasse. Deshalb: interface Tier <<Interface>> Comparable Elefant
InsertionSort für Objekte Interfaces ähneln abstrakten Klassen, die ausschließlich abstrakte Methoden besitzen (keine Attribute und keine implementierten Methoden). Unterschied: Eine Klasse kann mehrere Interfaces implementieren, aber nur eine Klasse (direkt) erweitern. Tier <<Interface>> Comparable Elefant
InsertionSort für Objekte public interface Comparable { public int compareto(object o); Interface Comparable war bis Java-Version 1.4 so im Paket java.lang definiert.
InsertionSort für Objekte public class Punkt { public double x; public double y; public Punkt(double myx, double myy) { x = myx; y = myy; /* gibt Wert < 0 fuer (this < p), Wert > 0 fuer (this > p) und 0 fuer (this = p) zurueck */ public int compareto(punkt p) { if (this.x < p.x) return -1; if (this.x == p.x) { if (this.y < p.y) return -1; if (this.y == p.y) return 0; return 1;
InsertionSort für Objekte public class Punkt implements Comparable { public double x; public double y; public Punkt(double myx, double myy) { x = myx; y = myy; /* gibt Wert < 0 fuer (this < p), Wert > 0 fuer (this > p) und 0 fuer (this = p) zurueck */ public int compareto(punkt p) { Compiler: "The type Punkt must implement the inherited abstract method Comparable.compareTo(Object)" if (this.x < p.x) return -1; if (this.x == p.x) { if (this.y < p.y) return -1; if (this.y == p.y) return 0; return 1;
InsertionSort für Objekte public class Punkt implements Comparable { public double x; public double y; public Punkt(double myx, double myy) { x = myx; y = myy; /* gibt Wert < 0 fuer (this < p), Wert > 0 fuer (this > p) und 0 fuer (this = p) zurueck */ public int compareto(object o) { Punkt p = (Punkt) o; if (this.x < p.x) return -1; if (this.x == p.x) { if (this.y < p.y) return -1; if (this.y == p.y) return 0; return 1;
InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { Punkt[] a = new Punkt[4]; a[0] = new Punkt(1, 2); a[1] = new Punkt(1, 1); a[2] = new Punkt(3, 1); a[3] = new Punkt(4, 3); insertionsort(a); for (int i = 0; i < a.length; i++) System.out.println(a[i].x + " " + a[i].y); public static void insertionsort(comparable[] a) { //...
InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { Punkt[] a = new Punkt[4]; a[0] = new Punkt(1, 2); a[1] = new Punkt(1, 1); a[2] = new Punkt(3, 1); a[3] = new Punkt(4, 3); insertionsort(a); Hier wird Feld mit Punkt-Objekten übergeben Hier wird Feld mit Comparable-Objekten entgegengenommen for (int i = 0; i < a.length; i++) System.out.println(a[i].x + " " + a[i].y); public static void insertionsort(comparable[] a) { //...
InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { Punkt[] a = new Punkt[4]; a[0] = new Punkt(1, 2); a[1] = new Punkt(1, 1); a[2] = new Punkt(3, 1); a[3] = new Punkt(4, 3); insertionsort(a); Ausgabe: 1.0 1.0 for (int i = 0; i < a.length; i++) System.out.println(a[i].x + " " + a[i].y); 1.0 2.0 3.0 1.0 public static void insertionsort(comparable[] a) { //... 4.0 3.0
InsertionSort für Objekte +m:punkt GeoFigur +GeoFigur(Punkt) +getflaeche():double +compareto(geofigur):int -a:double Quadrat +Quadrat(Punkt, double) +getflaeche():double +seta(double) +geta():double -r:double Kreis +Kreis(Punkt, double) +getflaeche():double +setr(double) +getr():double
InsertionSort für Objekte <<Interface>> Comparable +compareto(object):int +m:punkt GeoFigur +GeoFigur(Punkt) +getflaeche():double +compareto(object):int -a:double Quadrat +Quadrat(Punkt, double) +getflaeche():double +seta(double) +geta():double -r:double Kreis +Kreis(Punkt, double) +getflaeche():double +setr(double) +getr():double
Klasse GeoFigur in Java: InsertionSort für Objekte public class GeoFigur { public Punkt m; public GeoFigur(Punkt mym) { m = mym; public double getflaeche() { return 0.0; public int compareto(geofigur key) { if (this.getflaeche() > key.getflaeche()) return 1; if (this.getflaeche() < key.getflaeche()) return -1; return 0;
Klasse GeoFigur in Java: InsertionSort für Objekte public class GeoFigur implements Comparable { public Punkt m; public GeoFigur(Punkt mym) { m = mym; public double getflaeche() { return 0.0; public int compareto(object o) { GeoFigur key = (GeoFigur) o; if (this.getflaeche() > key.getflaeche()) return 1; if (this.getflaeche() < key.getflaeche()) return -1; return 0;
Sortieren: InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { GeoFigur[] a = new GeoFigur[4]; a[0] = new Quadrat(new Punkt(0.0, 0.0), 2.0); a[1] = new Quadrat(new Punkt(1.0, 2.0), 4.0); a[2] = new Kreis(new Punkt(2.0, 0.0), 2.0); a[3] = new Kreis(new Punkt(0.0, 1.0), 4.0); insertionsort(a); for (int i = 0; i < a.length; i++) System.out.println(a[i].getClass().toString() + " " + a[i].getflaeche()); public static void insertionsort(geofigur[] a) { //...
Sortieren: InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { GeoFigur[] a = new GeoFigur[4]; a[0] = new Quadrat(new Punkt(0.0, 0.0), 2.0); a[1] = new Quadrat(new Punkt(1.0, 2.0), 4.0); a[2] = new Kreis(new Punkt(2.0, 0.0), 2.0); a[3] = new Kreis(new Punkt(0.0, 1.0), 4.0); insertionsort(a); for (int i = 0; i < a.length; i++) System.out.println(a[i].getClass().toString() + " " + a[i].getflaeche()); public static void insertionsort(comparable[] a) { //...
Sortieren: InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { GeoFigur[] a = new GeoFigur[4]; a[0] = new Quadrat(new Punkt(0.0, 0.0), 2.0); a[1] = new Quadrat(new Punkt(1.0, 2.0), 4.0); a[2] = new Kreis(new Punkt(2.0, 0.0), 2.0); a[3] = new Kreis(new Punkt(0.0, 1.0), 4.0); insertionsort(a); Ausgabe: for (int i = 0; i < a.length; i++) System.out.println(a[i].getClass().toString() + " " + a[i].getflaeche()); public static void insertionsort(comparable[] a) { //... class Quadrat 4.0 class Kreis 12.566370614359172 class Quadrat 16.0 class Kreis 50.26548245743669
Sortieren: InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { GeoFigur[] a = new GeoFigur[4]; a[0] = new Punkt(0.0, 0.0); a[1] = new Quadrat(new Punkt(1.0, 2.0), 4.0); a[2] = new Kreis(new Punkt(2.0, 0.0), 2.0); a[3] = new Kreis(new Punkt(0.0, 1.0), 4.0); insertionsort(a); for (int i = 0; i < a.length; i++) System.out.println(a[i].getClass().toString() + " " + a[i].getflaeche()); public static void insertionsort(comparable[] a) { //...
Sortieren: InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { GeoFigur[] a = new GeoFigur[4]; a[0] = new Punkt(0.0, 0.0); a[1] = new Quadrat(new Punkt(1.0, 2.0), 4.0); a[2] = new Kreis(new Punkt(2.0, 0.0), 2.0); a[3] = new Kreis(new Punkt(0.0, 1.0), 4.0); insertionsort(a); Compiler: Type mismatch: cannot convert from Punkt to GeoFigur for (int i = 0; i < a.length; i++) System.out.println(a[i].getClass().toString() + " " + a[i].getflaeche()); public static void insertionsort(comparable[] a) { //...
Sortieren: InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { Comparable[] a = new Comparable[4]; a[0] = new Punkt(0.0, 0.0); a[1] = new Quadrat(new Punkt(1.0, 2.0), 4.0); a[2] = new Kreis(new Punkt(2.0, 0.0), 2.0); a[3] = new Kreis(new Punkt(0.0, 1.0), 4.0); insertionsort(a); for (int i = 0; i < a.length; i++) System.out.println(a[i]); public static void insertionsort(comparable[] a) { //...
Sortieren: InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { Ausgabe: Comparable[] a = new Comparable[4]; a[0] = new Punkt(0.0, 0.0); a[1] = new Quadrat(new Punkt(1.0, 2.0), 4.0); a[2] = new Kreis(new Punkt(2.0, 0.0), 2.0); a[3] = new Kreis(new Punkt(0.0, 1.0), 4.0); insertionsort(a); for (int i = 0; i < a.length; i++) System.out.println(a[i]); Exception in thread "main" java.lang.classcastexception: Quadrat cannot be cast to Punkt public static void insertionsort(comparable[] a) { at Punkt.compareTo(Punkt.java:12) //... at Sortierverfahren.insertionSort(Sortierverfahren.java:21) at Sortierverfahren.main(Sortierverfahren.java:9)
InsertionSort für Objekte public class Punkt implements Comparable { public double x; public double y; public Punkt(double myx, double myy) { x = myx; y = myy; /* gibt Wert < 0 fuer (this < p), Wert > 0 fuer (this > p) und 0 fuer (this = p) zurueck */ public int compareto(object o) { Punkt p = (Punkt) o; if (this.x < p.x) return -1; Ausgabe: if (this.x == p.x) { if (this.y < p.y) return -1; Exception if (this.y in thread == p.y) "main" return java.lang.classcastexception: 0; Quadrat cannot be cast to Punkt at Punkt.compareTo(Punkt.java:12) return 1; at Sortierverfahren.insertionSort(Sortierverfahren.java:21) at Sortierverfahren.main(Sortierverfahren.java:9)
InsertionSort für Objekte public class Punkt implements Comparable { public double x; public double y; public Punkt(double myx, double myy) { x = myx; y = myy; /* gibt Wert < 0 fuer (this < p), Wert > 0 fuer (this > p) und 0 fuer (this = p) zurueck */ public int compareto(object o) { Punkt p = (Punkt) o; if (this.x < p.x) return -1; if (this.x == p.x) { if (this.y < p.y) return -1; if (this.y == p.y) return 0; return 1;
InsertionSort für Objekte public class Punkt implements Comparable { public double x; public double y; public Punkt(double myx, double myy) { x = myx; y = myy; public int compareto(object o) { if (o instanceof Punkt) { Punkt p = (Punkt) o; if (this.x < p.x) return -1; if (this.x == p.x) { if (this.y < p.y) return -1; if (this.y == p.y) return 0; return 1; else { //?
InsertionSort für Objekte public class Punkt implements Comparable { public double x; public double y; public Punkt(double myx, double myy) { x = myx; y = myy; public int compareto(object o) { if (o instanceof Punkt) { Punkt p = (Punkt) o; if (this.x < p.x) return -1; if (this.x == p.x) { if (this.y < p.y) return -1; if (this.y == p.y) return 0; return 1; else { //? Es wäre gut, wenn wir wüssten, mit was für Objekten wir zu rechnen haben.
InsertionSort für Objekte public interface Comparable { public int compareto(object o);
InsertionSort für Objekte public interface Comparable<T> { public int compareto(t o); Seit Java Version 5 können generische Datentypen definiert werden.
InsertionSort für Objekte public interface Comparable<T> { public int compareto(t o); Seit Java Version 5 können generische Datentypen definiert werden. Typparameter (Platzhalter für den Namen einer Klasse)
InsertionSort für Objekte public interface Comparable<T> { public int compareto(t o); public class Punkt implements Comparable { //... public int compareto(object o) { Punkt p = (Punkt) o; if (this.x < p.x) return -1; if (this.x == p.x) { if (this.y < p.y) return -1; if (this.y == p.y) return 0; return 1;
InsertionSort für Objekte public interface Comparable<T> { public int compareto(t o); public class Punkt implements Comparable<Punkt> { //... public int compareto(punkt p) { if (this.x < p.x) return -1; if (this.x == p.x) { if (this.y < p.y) return -1; if (this.y == p.y) return 0; return 1;
InsertionSort für Objekte public interface Comparable<T> { public int compareto(t o); Für Typparameter wird Typargument (Punkt) eingesetzt. public class Punkt implements Comparable<Punkt> { //... public int compareto(punkt p) { if (this.x < p.x) return -1; if (this.x == p.x) { if (this.y < p.y) return -1; if (this.y == p.y) return 0; return 1;
InsertionSort für Objekte public interface Comparable<T> { public int compareto(t o); Für Typparameter wird Typargument (Punkt) eingesetzt. public class Punkt implements Comparable<Punkt> { //... public int compareto(punkt p) { if (this.x < p.x) return -1; if (this.x == p.x) { if (this.y < p.y) return -1; if (this.y == p.y) return 0; return 1; Punkte können nur mit Punkten verglichen werden!
InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { Punkt[] a = new Punkt[4]; a[0] = new Punkt(1, 2); a[1] = new Punkt(1, 1); a[2] = new Punkt(3, 1); a[3] = new Punkt(4, 3); insertionsort(a); for (int i = 0; i < a.length; i++) System.out.println(a[i].x + " " + a[i].y); public static void insertionsort(comparable[] a) { //...
InsertionSort für Objekte public class Sortierverfahren { public static void main(string[] args) { Punkt[] a = new Punkt[4]; a[0] = new Punkt(1, 2); a[1] = new Punkt(1, 1); a[2] = new Punkt(3, 1); a[3] = new Punkt(4, 3); insertionsort(a); for (int i = 0; i < a.length; i++) System.out.println(a[i].x + " " + a[i].y); public static <T extends Comparable<T>> void insertionsort (T[] a) { //...
InsertionSort für Objekte generische Methode: public static <T extends Comparable<T>> void insertionsort (T[] a) { //...
InsertionSort für Objekte generische Methode: Nimmt Feld mit Objekten einer Klasse T entgegen public static <T extends Comparable<T>> void insertionsort (T[] a) { //...
InsertionSort für Objekte generische Methode: Nimmt Feld mit Objekten einer Klasse T entgegen wobei T für beliebige Klasse steht, die diese Bedingung erfüllt public static <T extends Comparable<T>> void insertionsort (T[] a) { //...
InsertionSort für Objekte public class Sortierverfahren { public static <T extends Comparable<T>> void insertionsort(t[] a) { for (int j = 1; j < a.length; j++) { T key = a[j]; int i = j - 1; while (i >= 0 && a[i].compareto(key) > 0) { a[i + 1] = a[i]; i--; a[i + 1] = key;
Liste für Objekte public class Liste { public ListenElement head; public ListenElement insert(object k) { ListenElement x = new ListenElement(k, head); if (head!= null) { head.prev = x; head = x; return x; public class ListenElement { public Object key; public ListenElement prev; public ListenElement next; public ListenElement(Object k, ListenElement p) { key = k; next = p; prev = null;
Liste für Objekte public class List<T> { public ListItem<T> head; public ListItem<T> insert(t k) { ListItem<T> x = new ListItem<T>(k, head); if (head!= null) { head.prev = x; head = x; return x; public class ListItem<T> { public T key; public ListItem<T> prev; public ListItem<T> next; public ListItem (T k, ListItem<T> p) { key = k; next = p; prev = null;
Liste für Objekte public class ListenTest { public static void main(string[] args) { Liste l = new Liste(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); for (ListenElement p = l.head; p!= null; p = p.next) { if (p.key instanceof Punkt) { Punkt mypunkt = (Punkt) p.key; System.out.println(myPunkt.x + " " + mypunkt.y);
Liste für Objekte public class ListenTest { public static void main(string[] args) { List<Punkt> l = new List<Punkt>(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); for (ListItem<Punkt> p = l.head; p!= null; p = p.next) { System.out.println(p.key.x + " " + p.key.y);
Liste für Objekte public class ListenTest { public static void main(string[] args) { List<Punkt> l = new List<Punkt>(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); for (ListItem<Punkt> p = l.head; p!= null; p = p.next) { System.out.println(p.key.x + " " + p.key.y); Ausgabe: 1.0 0.0 2.0 2.0 0.0 1.0
Liste für Objekte public class ListenTest { public static void main(string[] args) { List<Punkt> l = new List<Punkt>(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(2.0)); for (ListItem<Punkt> p = l.head; p!= null; p = p.next) { System.out.println(p.key.x + " " + p.key.y);
Liste für Objekte public class ListenTest { public static void main(string[] args) { List<Punkt> l = new List<Punkt>(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(2.0)); for (ListItem<Punkt> p = l.head; p!= null; p = p.next) { System.out.println(p.key.x + " " + p.key.y); Compiler: "The method insert(punkt) in the type List<Punkt> is not applicable for the arguments (Double)"
Liste für Objekte public class ListenTest { public static void main(string[] args) { List<Object> l = new List<Object>(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(2.0)); for (ListItem<Object> p = l.head; p!= null; p = p.next) { System.out.println(p.key);
Liste für Objekte public class ListenTest { public static void main(string[] args) { List<Object> l = new List<Object>(); l.insert(new Punkt(0.0, 1.0)); l.insert(new Punkt(2.0, 2.0)); l.insert(new Punkt(1.0, 0.0)); l.insert(new Double(2.0)); for (ListItem<Object> p = l.head; p!= null; p = p.next) { System.out.println(p.key); Ausgabe: 2.0 (1.0, 0.0) (2.0, 2.0) (0.0, 1.0)
Schluss Generische Datentypen/Methoden erlauben es, Datenstrukturen/Algorithmen so zu implementieren, dass sie für verschiedene Datentypen angewandt werden können.