HeyMan HeyMan - 2 years ago 109
Java Question

Java get fields from raw type ArrayList

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 field.

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 access its fields.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download