开发者

Contents in an ArrayList get modified

I have a problem on a program I'm making that I honestly can't find a solution for. It seems the objects contained on a Java ArrayList collection are being modified without me programming such modifications.

The program as a whole is meant to basically create a random connection between two nodes on a 10x10 grid by moving through a path. This path is represented as an ArrayList collection of points in the grid, with the first index containing the first node's location (node A) and the last index containing the second node's location (node B). How I do this is by locating myself on A's location, and then moving randomly to contiguous points in the grid, repeating this in a while loop until B's location is reached.

Everything seems to work except that the "path" collection is altered somehow, such that every point in it ends up being the same as the last point to which I move, which is also B's location.

The method is as follows:

public void generateRandomPath()
{
    path = new ArrayList<Point>();
    path.add(pInitial);
    complete = false;

    while(!complete)
    {
        k = path.get(path.size()-1);
        d = selectDirection(k);

        GUI.log.append("==== Before the method. ==== \n");
        GUI.log.append(Integer.toString(path.get(path.size()-1).getX())+" - "+Integer.toString(path.get(path.size()-1).getY())+"\n");

        x = move(k, d);
        path.add(x);

        if(k.getX() == pEnd.getX() && k.getY() == pEnd.getY())
            complete = true;

    }
    GUI.log.append("Finished. \n");
}
  • "Point" are simply points, with an X and Y coordinate represented by integers.
  • "pInitial" is the point representing the location of node A.
  • "pEnd" is the point representing the location of node B.
  • "d" is the direction on which I'm going to move on this repetition. This can be either up, right, down, or left represented by an integer 1, 2, 3, and 4 respectively.
  • "k" is the last point in the path, which is the point to which it moved on the previous repetition.
  • "x" is the new point to which it moved on the current repetition.

So what it basically does is it grabs the last point in the path as reference, chooses a direction, and then moves to the point contiguous on that direction. Each repetition of the while loop should add a new point to path. However what ends up happening is that not only is this new point added, but every other point already in path takes the value of this last point added. By utilizing the log entries show above (GUI.log.append) I managed to see that path is being mysteriously altered inside the step:

x = move(k, d);

Which is the following method:

private Point move(Point n, int y)
{
    GUI.log.append("==== Inside the method. ==== \n");
    GUI.log.append(Integer.toString(path.get(path.size()-1).getX())+" - "+Integer.toString(path.get(path.size()-1).getY())+"\n");

    Point newP = n;
    if(y == 1)
        newP.setY(n.getY()-1);
    if(y == 2)
        newP.setX(n.getX()+1);
    if(y == 3)
        newP.setY(n.getY()+1);
    if(y == 4)
        newP.setX(n.getX()-1);

    GUI.log.append("==== After method. ==== \n");
    GUI.log.append(Integer.toString(path.get(path.size()-1).getX())+" - "+Integer.toString(path.get(path.size()-1).getY())+"\n");

    return newP;
}

Integer y is the direction as mentioned before. As you can see this method does not alter path in any way, yet the logs show it does. In this example node A was on the point X = 2, Y = 3. The log shows what the coordinates of the last point in path are. As you 开发者_Go百科can see, the coordinates of the last point in path take on the value of the coordinates of the new point, but this new point was not yet added to path.

Contents in an ArrayList get modified

I honestly don't know how this is happening. If anyone could think of a reason I would appreciate it very much if you could tell me.


Try

Point newP = new Point(n.getX(), n.getY());

instead of

 Point newP = n;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜