You can make an n-dimensional ArrayList, just like an n-dimensionaly Array, by putting ArrayLists into ArrayLists.
Here an example with 3 dimensions to show the concept.
public static void main(String args[]){
ArrayList<ArrayList<ArrayList<Integer>>> listOfListOfList = new ArrayList<ArrayList<ArrayList<Integer>>>();
int firstDimensionSize = 3;
int secondDimensionSize = 4;
int thirdDimensionSize = 5;
for (int i = 0; i < firstDimensionSize; i++) {
listOfListOfList.add(new ArrayList<ArrayList<Integer>>(vertices));
for (int j = 0; j < secondDimensionSize; j++) {
listOfListOfList.get(i).add(new ArrayList<Integer>());
for(int k = 0; k < thirdDimensionSize; k++) {
listOfListOfList.get(i).get(j).add(k);
}
}
}
}
Note that you can leave the <> empty after the new ArrayList<>. Java will infer the type (no matter how nested), since java 7 I believe. I just wrote them down in the example to show what type you are handling at every level, to make the example more clear. You can still write them down to make your code more readable.
Object
, to a parameterized type :ArrayList<Integer>
. – Rob Mar 9 '13 at 19:16