I have a class with these private members:
private ArrayList<ArrayList<ArrayList<CustomStuff>>> listOfPaths;
private int currentIndex;
I fill the array in a method like this:
listOfPaths.get(currentIndex).add(path); //path is ArrayList<CustomStuff>
All is fine so far.
Checking:
System.out.println(listOfPaths.get(currentIdx).get(listOfPaths.get(currentIdx).size() - 1).size());
Gives the right size.
Now: After the method finishes. There is only one object left in every single leaf in the ArrayList<ArrayList<ArrayList<CustomStuff>>>
So System.out.println(listOfPaths.get(anyValidIdx).get(anyValidIdx).size());
will always be 1!
By the way: listOfPaths.size()
and listOfPaths.get(anyValidIdx).size()
give the right sizes!
So only the third dimension of the array seem to shrink to a single object.
What is going wrong?
Background Story:
I have points on a matrix. Start and end marks. Between those marks I have paths. A path consists of steps.
so:
- A path is ArrayList<Step>
.
- Collection of different paths for the same start/end marks is: ArrayList<ArrayList<Step>>
.
- All collections on the matrix is ArrayList<ArrayList<ArrayList<Step>>>
.
I start with a pair of marks, and look for all the available paths. When searching I add every found path: listPaths.get(currentIndex).add(pathBetweenStartAndEnd)
So when I am finished fetching paths for a pair of marks, I increment the currentIndex and move to the next pair of marks and so on...
Complete code:
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);
}
}
}