2

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!

asked Nov 12, 2010 at 5:14

7 Answers 7

10

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.

answered Nov 12, 2010 at 5:21
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! didn't realize that! Thanks for the code + explanation. (most other answers are correct too, but yours had the most effort! :)
4

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.

answered Nov 12, 2010 at 5:18

Comments

2

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.

answered Nov 12, 2010 at 5:17

Comments

2

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());
}
answered Nov 12, 2010 at 5:25

Comments

2

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.

answered Nov 12, 2010 at 6:02

Comments

0

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.

answered Nov 12, 2010 at 5:18

Comments

0

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.

answered Dec 7, 2011 at 21:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.