problem with ArrayList in Java

淺唱寂寞╮ 提交于 2020-01-04 05:43:25

问题


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!


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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());
}



回答5:


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.




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/4161827/problem-with-arraylist-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!