While coding in Android, I needed an ArrayList of Points which I called wormPt. I initialized it through a loop.
ArrayList<Point> wormPt = new ArrayList<Point>();
Point pt = new Point();
.
.
.
private void initializeWorm() {
// TODO Auto-generated method stub
pt.x = 220;
pt.y = 300;
for (int i = 0; i <= 5; i++) {
wormPt.add(pt);
Log.d("wormdebug", wormPt.toString());
pt.x -= 5;
}
Log.d("wormdebug", wormPt.toString());
}
My last log.d should report points
(220,300)
(215,300)
(210,300)
(205,300)
(200,300)
(195,300)
Instead, all my points are (190, 300)
Here is my log data
11-21 23:48:11.549: D/wormdebug(3273): [Point(220, 300)]
11-21 23:48:11.600: D/wormdebug(3273): [Point(215, 300), Point(215, 300)]
11-21 23:48:11.600: D/wormdebug(3273): [Point(210, 300), Point(210, 300), Point(210, 300)]
11-21 23:48:11.600: D/wormdebug(3273): [Point(205, 300), Point(205, 300), Point(205, 300), Point(205, 300)]
11-21 23:48:11.600: D/wormdebug(3273): [Point(200, 300), Point(200, 300), Point(200, 300), Point(200, 300), Point(200, 300)]
11-21 23:48:11.600: D/wormdebug(3273): [Point(195, 300), Point(195, 300), Point(195, 300), Point(195, 300), Point(195, 300), Point(195, 300)]
11-21 23:48:11.630: D/wormdebug(3273): [Point(190, 300), Point(190, 300), Point(190, 300), Point(190, 300), Point(190, 300), Point(190, 300)]
11-21 23:48:14.669: W/KeyCharacterMap(3273): No keyboard for id 0
11-21 23:48:14.679: W/KeyCharacterMap(3273): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
I tried
Can't add element to ArrayList in for loop and others, but they don’t seem to be having the same problem. Any help will be appreciated.
Thanks in advance.
The problem is that your
ArrayListcontains several references to the same object. All you’re doing in the loop is adding the same reference and mutating the object.If you change the loop to create a new
Pointon each iteration, it will work:It’s very important to understand the difference between variables, objects and references.
ptis a variable. Its value is a reference to aPointobject. Unless you ask for a new object, Java won’t create one for you. For example:Note that this isn’t associating the a and b variables with each other – it’s just giving them the same value (the same reference). So you could later change
ato be a reference to a differentPoint, and that wouldn’t changeb:In this case, it’s a bit like giving 10 different people a piece of paper with your home address on. If one of those people visits the address and paints the front door green, then another of them visits the address, they’ll see a green front door.