Java ArrayList的对象消失

Java ArrayList的对象消失,java,arraylist,Java,Arraylist,我与这些私人成员一起上课: private ArrayList<ArrayList<ArrayList<CustomStuff>>> listOfPaths; private int currentIndex; 大小合适 现在:方法完成后。ArrayList中的每个叶中只剩下一个对象 所以System.out.println(listofPath.get(anyvalidix).get(anyvalidix.size())将始终为1 顺便说一下:listOf

我与这些私人成员一起上课:

private ArrayList<ArrayList<ArrayList<CustomStuff>>> listOfPaths;
private int currentIndex;
大小合适

现在:方法完成后。
ArrayList中的每个叶中只剩下一个对象

所以
System.out.println(listofPath.get(anyvalidix).get(anyvalidix.size())将始终为1

顺便说一下:
listOfPaths.size()
listOfPaths.get(anyvalidix).size()
给出正确的大小

因此,只有阵列的第三维度似乎缩小为单个对象

出什么事了

背景故事: 我在矩阵上有点。开始和结束标记。在这些标记之间我有路径。路径由步骤组成。 所以:
-路径是
ArrayList

-相同开始/结束标记的不同路径的集合为:
ArrayList

-矩阵上的所有集合都是
ArrayList

我从一对标记开始,查找所有可用的路径。搜索时,我添加找到的每个路径:
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);
        }
    }
}
import java.util.ArrayList;
公共类求解器{
受保护的游戏板;
私有静态数组列表路径;
私有int电流对;
公共游戏板getSolvedBoard(){
solve();
返回板;
}
公共空白收进板(游戏板){
this.board=董事会;
}
公共解算器(游戏板)
{
超级();
this.board=董事会;
}
受保护的void solve()
{
ListofPath=new ArrayList();
电流对=0;
for(单元对:board.getPairs())
{
System.out.printf(“获取%d:\n的路径”,pair.getFirstCell().getValue());
ArrayList路径=新建ArrayList();
add(pair.getFirstCell());
添加(新的ArrayList());
DFS(pair.getFirstCell()、pair.getSecondCell()、new ArrayList()、path);
System.out.println(“-------------------------------------------------------------”;
++电流对;
}
System.out.println(listofPath.get(0.get(0.size));
//System.out.println(路径列表get(2.get(205.get(1));
}
受保护的静态阵列列表getSonsForCellOnBoard(BoardCell单元、GameBoard板)
{
int row=cell.getRow(),
column=cell.getColumn();
ArrayList邻居=新的ArrayList();
如果(行>0)
add(board.getCellAtIndex(第1行,第2列));
if(行0)
add(board.getCellAtIndex(行,列-1));
if(列
在Java中,非原语类型(您案例中存储在path中的列表)是通过引用而不是通过值传递的

当你打电话时:

DFS(son, target, visited, path);
最终,在递归结束时,您将路径存储在
路径列表中

但在你这么做之后:

visited.remove(son);
path.remove(path.size() - 1);
因为路径是作为引用传递的,所以对它的任何更改都会影响存储在路径列表中的路径

因此,更换此部件(顺便说一句,在这种情况下,温度是冗余的):

ArrayList temp=new ArrayList();
温度=路径;
获取(当前对).add(临时)路径列表;
使用此选项(仅复制路径列表):

ArrayList temp=new ArrayList();
用于(BoardCell bc:路径)
温度添加(bc);
获取(当前对).add(临时)路径列表;

并在代码中寻找更多类似问题的地方

在Java中,非基本类型(在您的示例中存储在path中的列表)是通过引用而不是通过值传递的

当你打电话时:

DFS(son, target, visited, path);
最终,在递归结束时,您将路径存储在
路径列表中

但在你这么做之后:

visited.remove(son);
path.remove(path.size() - 1);
因为路径是作为引用传递的,所以对它的任何更改都会影响存储在路径列表中的路径

因此,更换此部件(顺便说一句,在这种情况下,温度是冗余的):

ArrayList temp=new ArrayList();
温度=路径;
获取(当前对).add(临时)路径列表;
使用此选项(仅复制路径列表):

ArrayList temp=new ArrayList();
用于(BoardCell bc:路径)
温度添加(bc);
获取(当前对).add(临时)路径列表;

并在代码中寻找更多类似问题的地方

为什么有一个
ArrayList
ArrayList
ArrayList
?必须有一个更好的数据结构。
ArrayList
如果你看到这样的代码,你真的需要重新考虑你的结构
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);