C
Container -.-
Gast
ich verstehe mein Problem nicht
die klasse book (soweit auch ohne fehler)
so nun zur klasse die die fehler verursacht
so lange zahl nicht 6 ist, ist das kein problem, aber wenn ich zahl = 6 setzt funtzt es nicht er gibt folgende exception
Exception in thread "main" java.lang.ClassCastException: java.util.AbstractList$Itr cannot be cast to Book
at Exercise.part6(Exercise.java:34) ((Book)it).ausgabe();
at Exercise.<init>(Exercise.java:23) this.part6();
at Exercise.main(Exercise.java:42) Exercise e = new Exercise(zahl);
ihr braucht eigendlich kaum qc lesen, denke es liegt an meinem ((Book)it).ausgabe(); ich habe mich noch nicht lange mit container beschäftig, deswegen wird da wohl auch der fehler liegen
die klasse book (soweit auch ohne fehler)
Code:
public class Book {
private String author,title,edition;
public Book(String author1, String title1,String edition1){
this.author=author1;
this.title=title1;
this.edition=edition1;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
public void ausgabe(){
System.out.println(author+" "+ title+" "+edition);
}
}
Code:
import java.util.ArrayList;
import java.util.Iterator;
public class Exercise {
Book[] einBucharray= new Book[6];
ArrayList<Book> arl= new ArrayList<Book>();
public Exercise(int eineZahl){
Book b1= new Book("Goethe","Faust","20000 Sück");
Book b2= new Book("Fontane","Effi Briest","20000 Sück");
Book b3= new Book("Schiller","Wilhelm Tell","20000 Sück");
Book b4= new Book("E.T.A Hoffman","Der Sandmann","20000 Sück");
Book b5= new Book("Die Katholische Kirche","Die Bibel","20000000 Sück");
Book b6= new Book("Die Iluminaten","Zensiert","23 Sück");
einBucharray[0]=b1;
einBucharray[1]=b2;
einBucharray[2]=b3;
einBucharray[3]=b4;
einBucharray[4]=b5;
einBucharray[5]=b6;
if (eineZahl==6){
this.part6();
}
}
public void part6(){
for (int i=0; i<einBucharray.length;i++){
arl.add(einBucharray[i]);
}
Iterator<Book> it = arl.iterator();
while (it.hasNext()) {
it.next();
((Book)it).ausgabe();
}
}
public static void main(String[] args) {
int zahl=0;
//zahl=Integer.parseInt(args[0]);
Exercise e = new Exercise(zahl);
// Ausgabe klappt!
for(int i=0;i<e.einBucharray.length; i++){
e.einBucharray[i].ausgabe();
}
}
}
Exception in thread "main" java.lang.ClassCastException: java.util.AbstractList$Itr cannot be cast to Book
at Exercise.part6(Exercise.java:34) ((Book)it).ausgabe();
at Exercise.<init>(Exercise.java:23) this.part6();
at Exercise.main(Exercise.java:42) Exercise e = new Exercise(zahl);
ihr braucht eigendlich kaum qc lesen, denke es liegt an meinem ((Book)it).ausgabe(); ich habe mich noch nicht lange mit container beschäftig, deswegen wird da wohl auch der fehler liegen