HeyMan HeyMan - 1 year ago 43
Java Question

Java ArrayList get the x error

i have a problem with taking the x from an ArrayList in a for.
Let me explain better with this code:

private ArrayList pointStorer = new ArrayList();

I first declare the ArrayList

public void mouseEntered(MouseEvent e){

for(int i = 0; i < pointStorer.size(); i++){
if(pointStorer.get(i).x <= e.getX()){
check = true;

Then when i try to pass the ArrayList and for every element get the x, it doesn't work, Java says that 'cannot find simbol' (the x).

Thank you in advance for your help.

Answer Source

When you get an element from your pointStorer you need to cast it to Point (or whatever class you are using), otherwise the program will not know its type and it will not know it has an x parameter.

But it would be better to explicitly define the array type, like this:

private List<Point> pointStorer = new ArrayList<Point>();

this way when you use get it will directly return a Point object and you would not have to cast it to use its parameters.