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.
wormPt.add()is cloningpt.