1

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!

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
KarinaSmrn
  • 77
  • 7
  • 2
    Why the method `showElements` is in the `Figura` class? It doesn't make sens. – Mateusz Korwel Nov 30 '15 at 21:24
  • a gdzie jeszcze ma być? W innym public classie to też nie za bardzo ma sens – KarinaSmrn Nov 30 '15 at 22:13
  • **PL**: Jeśli już musisz mieć taką metodę to możesz np. zrobić ją statyczną, albo stworzyć klasę `FiguraContainer` która ma listę obiektów `Figura` + metody które operują na tej liście jak `showElements`. **EN**: If you must have that method, you can make it static, or create a `FiguraContainer` class with a list + few method that operate on that list like `showElements`. – Mateusz Korwel Nov 30 '15 at 22:28
  • ok, got it. In case some List specific methods should be invoked, I'll create a separate class. Thank you! Dzięki! – KarinaSmrn Nov 30 '15 at 22:32

3 Answers3

1

kolekcje is a reference to ArrayList. Method showElements is a method on Figura object.

You are trying to access showElements on ArrayList, which is not available.

You will be able to access it on Figura instances, like:

kolekcje[0].showElements(kolekcje);

Also, based on what showElements is doing, you may want to consider making it static and access it like:

Figura.showElements(kolekcje);
Aragorn
  • 5,021
  • 5
  • 26
  • 37
1

In order to be able to call the showElements method, you need to invoke it on a Figura object. So

kolekcje.showElements(kolekcje);

will not work because kolekcje is a List, not a Figura.

Since you want to display a list of Figuras, & not just one; you would better make the showElements method static. You can call it this way:

Figura.showElements(arrayOfFiguras);

Figura.showElements()

// Display all the items of a Figura List
public static void showElements (List <Figura> m) { 
    for (int i = 0; i < m.size(); i++){
        System.out.println(m.get(i));
    }
}
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
0

You are calling the showElements() method on the ArrayList, not on the Figura object. There are two solutions to this problem, the better one in my opinion being to make the method static. As follows:

public static void showElements (List <Figura> m) { 
Figura p =  m.get(index);
for (int i = 0; i < m.size(); i++){
    System.out.println(m.get(i));
}

Finally, change the method call from:

kolekcje.showElements(kolekcje);

to:

Figura.showElements(kolekcje);

Making the method static is important because the showElements() method is completely unrelated to the instance of the Figura object you are calling it on. But rather, it is fully dependent on the List you are passing in.

You could read more about why to use static methods here.

Community
  • 1
  • 1
Bimde
  • 722
  • 8
  • 20