java - ArrayList的对象消失

标签 java arraylist

我有一个包含这些私有(private)成员的类(class):

private ArrayList<ArrayList<ArrayList<CustomStuff>>> listOfPaths;
private int currentIndex;

我用这样的方法填充数组:

listOfPaths.get(currentIndex).add(path); //path is ArrayList<CustomStuff>

到目前为止一切都很好。
检查:

System.out.println(listOfPaths.get(currentIdx).get(listOfPaths.get(currentIdx).size() - 1).size());

给出合适的尺寸。

现在:方法完成后。 ArrayList<ArrayList<ArrayList<CustomStuff>>> 中的每一片叶子中都只剩下一个对象。

所以System.out.println(listOfPaths.get(anyValidIdx).get(anyValidIdx).size());永远是 1!

顺便说一下:listOfPaths.size()listOfPaths.get(anyValidIdx).size()给出正确的尺寸!

因此,只有数组的第三个维度似乎缩小为单个对象。

出了什么问题?

背景故事: 我在矩阵上有点。开始和结束标记。在这些标记之间我有路径。路径由步骤组成。 所以:
- 路径是 ArrayList<Step> .
- 相同开始/结束标记的不同路径的集合是:ArrayList<ArrayList<Step>> .
- 矩阵上的所有集合为 ArrayList<ArrayList<ArrayList<Step>>> .

我从一对标记开始,寻找所有可用的路径。搜索时,我添加每个找到的路径:listPaths.get(currentIndex).add(pathBetweenStartAndEnd)
因此,当我完成获取一对标记的路径时,我会增加 currentIndex 并移至下一对标记,依此类推...

完整代码:

import java.util.ArrayList;

public class Solver {

    protected GameBoard board;

    private static ArrayList<ArrayList<ArrayList<BoardCell>>> listOfPaths;
    private int currentPair;

    public GameBoard getSolvedBoard() {
        solve();
        return board;
    }

    public void setBoard(GameBoard board) {
        this.board = board;
    }

    public Solver(GameBoard board)
    {
        super();

        this.board = board;
    }


    protected void solve()
    {
        listOfPaths = new ArrayList<ArrayList<ArrayList<BoardCell>>>();
        currentPair = 0;

        for(CellPair pair : board.getPairs())
        {
            System.out.printf("Getting paths for %d:\n", pair.getFirstCell().getValue());

            ArrayList<BoardCell> path = new ArrayList<BoardCell>();
            path.add(pair.getFirstCell());

            listOfPaths.add(new ArrayList<ArrayList<BoardCell>>());

            DFS(pair.getFirstCell(), pair.getSecondCell(), new ArrayList<BoardCell>(), path);

            System.out.println("--------------------------------------------------");

            ++currentPair;
        }

        System.out.println(listOfPaths.get(0).get(0).size());
        //System.out.println(listOfPaths.get(2).get(205).get(1));
    }

    protected static ArrayList<BoardCell> getSonsForCellOnBoard(BoardCell cell, GameBoard board)
    {
        int row     = cell.getRow(),
            column  = cell.getColumn();

        ArrayList<BoardCell> neighbors = new ArrayList<BoardCell>();

        if(row > 0)
            neighbors.add(board.getCellAtIndex(row - 1, column));
        if(row < board.getNumberOfRows() - 1)
            neighbors.add(board.getCellAtIndex(row + 1, column));
        if(column > 0)
            neighbors.add(board.getCellAtIndex(row, column - 1));
        if(column < board.getNumberOfColumns() - 1)
            neighbors.add(board.getCellAtIndex(row, column + 1));

        return neighbors;
    }

    private void DFS(   BoardCell source, 
                        BoardCell target, 
                        ArrayList<BoardCell> visited, 
                        ArrayList<BoardCell> path   )
    {
        if(source.getRow() == target.getRow() && source.getColumn() == target.getColumn())
        {
            System.out.printf("PATH: %d: ", path.size());
            System.out.println(path);

            ArrayList<BoardCell> temp = new ArrayList<BoardCell>();
            temp = path;

            listOfPaths.get(currentPair).add(temp);

            System.out.println(listOfPaths.get(currentPair).get(listOfPaths.get(currentPair).size() - 1).size());

            return; 
        }

        for(BoardCell son : Solver.getSonsForCellOnBoard(source, board))
        {
            if(visited.contains(son))
                continue;

            if(son != target &&
                    son.getType() == BoardCell.BoardCellType.BoardCell_AnchorCell)
                continue;

            path.add(son);
            visited.add(son);

            DFS(son, target, visited, path);

            visited.remove(son);
            path.remove(path.size() - 1);
        }
    }
}

最佳答案

在Java中,非原始类型(在您的情况下存储在路径中的列表)是通过引用而不是值传递的。

当你打电话时:

DFS(son, target, visited, path);

最终在递归结束时,您将路径存储在 listOfPaths 中。

但在你这样做之后:

visited.remove(son);
path.remove(path.size() - 1);

由于路径是作为引用传递的,对其进行的任何更改都会影响存储在路径列表中的路径。

所以替换它(顺便说一句,在这种情况下 temp 是多余的):

ArrayList<BoardCell> temp = new ArrayList<BoardCell>();
temp = path;

listOfPaths.get(currentPair).add(temp);

这样(只需复制路径列表):

ArrayList<BoardCell> temp = new ArrayList<BoardCell>(); 
for (BoardCell bc : path)   
     temp.add(bc);

listOfPaths.get(currentPair).add(temp);

并在代码中查找更多存在类似问题的地方。

关于java - ArrayList的对象消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16500822/

相关文章:

java - 在 TestNG 测试中获取 org.hibernate.LazyInitializationException

java - 使用扫描仪输入实例化变量后执行停止

javascript - 使用两个条件从对象数组中过滤数据

java - ListView 未按预期生成

java - 使用 ArrayList<Model> 将值存储到 Java 中的 MongoDB

java - 在 Java 中处理 N 个线程上的 M 个慢速计算

java - 添加框架内容时出现空指针异常

java - Spring Boot @RestControllerAdvice 没有捕获自定义异常

Java - 在eclipse中导出到可执行jar文件后读取文件

java - Java 中的动态数组 - 我有哪些选择?