I'm trying to print out elements from the List in the main method. However I have a problem, because Eclipse says that the method I've implemented in a superclass is not defined for my type. There is the superclass, there are three other which extend this one and I've imported java.util.List and java.util.ArrayList; The reason I need to implement method is because my mentor asked that, however I know just using for-loop will be enough in the main method. Superclass:
import java.util.ArrayList;
import java.util.List;
public class Figura {
private int x;
private int y;
private String name;
private int index;
public Figura (String name, int x, int y) {
this.name = name;
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(Figura p) {
System.out.println(p.toString());
}
public void showElements (List <Figura> m) {
Figura p = m.get(index);
for (int i = 0; i < m.size(); i++){
System.out.println(m.get(i));
}
}
}
I will not show here all other classes I've created (there classes are simple figures, which extend all the superclass). But the main problem is here:
import java.util.ArrayList;
import java.util.List;
public class zadanie2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List <Figura> kolekcje = new ArrayList<Figura>(10);
Punkt pr1 = new Punkt("Prostokat", 10,148,"Ciemne");
kolekcje.add(0,new Prostokat(pr1,42));
Punkt kolo1 = new Punkt("Kolo", 41,23,"Malinowe");
kolekcje.add(1,new Kolo(kolo1, 126));
Punkt pu31 = new Punkt("Punkt 3D", 45, 23,"Pomaranczowe");
kolekcje.add(2, new punkt3D(pu31, 451));
Punkt kw1 = new Punkt("Kwadrat", 215, 521,"Szary");
kolekcje.add(3, new Kwadrat(kw1, 620));
Punkt wk1 = new Punkt("Wektor", 52, 13,"Brazowy");
kolekcje.add(4, new Wektor(wk1, 41,52));
Punkt ec1 = new Punkt("Eklipsa", 52, 301, "Kremowy");
kolekcje.add(5, new Eclipsa(ec1, 22));
Punkt pr2 = new Punkt("Prostokat", 63,40,"Bialy");
kolekcje.add(6, new Prostokat(pr2,310));
Punkt kolo2 = new Punkt("Kolko",52,314,"Rozowe");
kolekcje.add(7, new Kolo(kolo2, 52));
Punkt pu32 = new Punkt("Punkcik 3D", 52,63,"Ciemno-brazowe");
kolekcje.add(8, new punkt3D(pu32, 412));
Punkt kw2 = new Punkt("Kwadracik", 52, 631,"Oliwkowy");
kolekcje.add(9, new Kwadrat(kw2,541));
Punkt wk2 = new Punkt("Wektor", 12, 71, "Czarne");
kolekcje.add(10, new Wektor(wk2, 41, 23));
int size = kolekcje.size();
//for (int i = 0; i< kolekcje.size(); i++) {
// System.out.println(kolekcje.get(i));
//}
kolekcje.showElements(kolekcje);
}
What is commented (for loop), works just fine, however showElements(List <Figura> m)
method doesn't work and Eclipse shows that this method is not defined to the type List<Figura>
.
Also, it suggest to cast the method like this
((Figura) kolekcje).showElements(kolekcje);
But it doesnt work either and shows the error Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to zadaniedomoweZadanie1.Figura
Please help!