0

Im using a list of objects (Area) in java, which i defined as so:

    public static List<Area> areaList = new ArrayList<Area>();

I add content to my list as so:

    areaList.add(new Area(px, py, pz, radius, wl));

then i access the list, to check each of the Area's within it like so:

for (int i = 1; i < areaList.size(); i++) {
    System.out.println(areaList.get(i).somevariable));
}

(ignore the 'somevariable' and i also didnt use println(), this was for example, the way i accessed using:

areaList.get(i)

is whats important here)

But it returns all the objects inside the List as having the same values - that of the last one accessed.

Can anyone tell me where im going wrong?

user825962
  • 499
  • 1
  • 5
  • 9
  • 2
    The error is not in the code you show. Please create a complete but minimal example that exposes the error (see http://sscce.org). – Björn Pollex Nov 30 '11 at 14:44
  • How do you fill the list? Where do `px`, `py`, `pz`, `radius` and `wl` come from? Just a note: Java uses 0 based indexing (see your for loop). – H-Man2 Nov 30 '11 at 14:45
  • 1
    Are you intentionally skipping element 0? Also consider using the for each construct, `for(Area a : areaList) { println(a.somevariable); }` – Dilum Ranatunga Nov 30 '11 at 14:48
  • the variables arent too important, as they are for other parts of the program, it is a bukkit plugin, i have uploaded the complete source code here: http://hyperarray.x10.mx/minecraft/plugins/main.zip which contains 3 classes, but what other parts of the code would you need? i have given you the only part of the code which writes to the list? – user825962 Nov 30 '11 at 15:25
  • I had a look through the code. You can find my answer below. – H-Man2 Nov 30 '11 at 20:04

4 Answers4

1

It's not the solution but you should begin your loop with i=0

Fred
  • 4,846
  • 1
  • 23
  • 21
0

You can try printing out hashcode() of each object to see if they are really the same, provided that is not overridden.

Also you are starting your iteration at 1, you need to start at 0 to iterate over the entire collection.

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • I believe ArrayList should only look at the equals method. Naturally, the hashCode method should be written accordingly, but that wouldn't be the source of the error here – Miquel Nov 30 '11 at 14:47
  • Right, but `hashcode` gives you something to print out to see if they are actually referring to the same object. – Garrett Hall Nov 30 '11 at 15:06
0

Are you sure you want to start with i=1 instead of i = 0? This might be relevant if you are running your tests with only two objects

Miquel
  • 15,405
  • 8
  • 54
  • 87
0

I looked through your uploaded code and found the error.

You declared every field in the class Area as static:

public class Area {
    public static int posx;
    public static int posy;
    public static int posz;
}

The fields are therefore equals for all instances, because there is just one posx field for the class Area (read this question and the answers for details). Because of this every write to, e. g., posx overrides the old value. Remove the static keyword and then all instances of the class Area have their own instance variable.

You should also think about making them private and provide getter and setter methods.

I haven't looked in detail through your code, but there are several places where you used static variables. You should revisit this.

Community
  • 1
  • 1
H-Man2
  • 3,169
  • 20
  • 19