Reputation: 7400
I have issues getting my ArrayList to add properly. When I print the ArrayList after the for loop has finished, the ArrayList is the correct length, but every element is the same (the last Coordinate that was created).
Can someone fix (and explain) the code below?
public class test {
private static ArrayList<Coordinate> mOrigCoords;
private static ArrayList<Coordinate> mNewCoords;
private static int mListSize;
private static int mPixelsX;
public static void main(String[] args)
{
mOrigCoords = new ArrayList<Coordinate>();
mNewCoords = new ArrayList<Coordinate>();
mPixelsX = 480;
int j = 0;
Coordinate newCoord = new Coordinate(0,0);
for(int i = 0; i < 96; i++)
{
j = j + 5;
newCoord.setX(j);
newCoord.setY((int)(Math.random()*300));
mOrigCoords.add(newCoord);
}
mListSize = mOrigCoords.size();
for(int n = 0; n < mListSize; n++)
{
System.out.println("test " + mOrigCoords.get(n).toString());
}
}
}
Thanks in advance for the help!
Upvotes: 2
Views: 3393
Reputation: 11
Thats because X and Y,which are properties of "Coordinate" class, are static properties, so objects have one copy of them in memory. All instances are pointing same address. You shouldnt use "static" keyword so they can use own copy and have different values.
Upvotes: 0
Reputation: 3587
In the above code you are adding the same instance every time.You have created only one instance and adding it again and again till the loop iteration.
So make make a new instance of Coordinate class every time for putting it in the list.It will will give you different print result.
As in the other solution it is shown. Hope this will help you.
Upvotes: 2
Reputation: 19344
your problem here is that your never creating new instances of your Coorddinates. So each time you modify newCoord your modifying the same instance. Arraylist doesn't do a copy of the object, it just stores its reference in the list, which means if you don't create a new instance, your always adding the same
try this
Coordinate newCoord;
for(int i = 0; i < 96; i++)
{
j = j + 5;
//newCoord.setX(j);
//newCoord.setY((int)(Math.random()*300));
mOrigCoords.add(new Coordinate(j,(int)(Math.random()*300)));
}
mListSize = mOrigCoords.size();
for(int n = 0; n < mListSize; n++)
{
System.out.println("test " + mOrigCoords.get(n).toString());
}
Upvotes: 2
Reputation: 76508
Instead of
Coordinate newCoord = new Coordinate(0,0);
for(int i = 0; i < 96; i++)
{
j = j + 5;
newCoord.setX(j);
newCoord.setY((int)(Math.random()*300));
mOrigCoords.add(newCoord);
}
you should have
Coordinate newCoord = null;
for(int i = 0; i < 96; i++)
{
newCoord = new Coordinate(0,0);
j = j + 5;
newCoord.setX(j);
newCoord.setY((int)(Math.random()*300));
mOrigCoords.add(newCoord);
}
This way, the arrayList will hold many objects instead of only one. All the elements in your ArrayList are pointing to the same object, that was the cause of trouble.
Upvotes: 10
Reputation: 39485
With each iteration, you are re-adding the same instance of your Coordinate
. You then reset the values on that instance. At the end of your loop, you have 96 indexes pointing to the same object, which has been updated with each iteration.
Upvotes: 0
Reputation: 3671
you have to do like this :
Coordinate newCoord;
for(int i = 0; i < 96; i++)
{
newCoord = new Coordinate(0,0);
...
Because in your case you are setting the same object (newCoord) each time.
Upvotes: 4
Reputation: 21883
Thats because you are adding the same coordinate object every time in your loop. you need to create new coordinate objects for each iteration of the loop.
Upvotes: 2