我有一个包含这些私人成员的课程:
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);
}
}
}