Hallöchen🙂
Vielleicht kann mir ja jemand weiterhelfen.
Ich soll eine Methode schreiben (public static Object max(Object[] a)), welche mir das größte Objekt aus einem Array zurück gibt.
Die Methode sieht bislang so aus:
Nun weiß ich nicht genau wie ich diese Methode aufrufen soll, mein Code sieht bislang so aus: (Funktioniert aber nicht)
Bis lang wirft mir das Programm immer eine ClassCastException.
Liebe Grüßchen
Katchy
Vielleicht kann mir ja jemand weiterhelfen.
Ich soll eine Methode schreiben (public static Object max(Object[] a)), welche mir das größte Objekt aus einem Array zurück gibt.
Die Methode sieht bislang so aus:
Code:
public static Object max(Comparable[]a){
Object max = new Object();
for (int i= 1; i < a.length - 1; i++){
if((a[i]).compareTo(a[i+1]) > 0){
max = a[i];
}else{
max = a[i+1];
}
}
return max;
}
Nun weiß ich nicht genau wie ich diese Methode aufrufen soll, mein Code sieht bislang so aus: (Funktioniert aber nicht)
Java:
public class Ex1_1 {
private static Shape[] shapes;
public static void main (String[] args) throws CloneNotSupportedException{
Comparable[]shapes = (Comparable[]) new Shape[2];
shapes[0] = new Octagon(100,40,50);
shapes[0].compareTo(shapes[1]);
System.out.println(max(shapes));
public static Object max(Comparable[]a){
Object max = new Object();
for (int i= 1; i < a.length - 1; i++){
if((a[i]).compareTo(a[i+1]) > 0){
max = a[i];
}else{
max = a[i+1];
}
}
return max;
}
}
Bis lang wirft mir das Programm immer eine ClassCastException.
Liebe Grüßchen
Katchy
Zuletzt bearbeitet von einem Moderator: