You can make an n-dimensional ArrayList, like an n-dimensional array, by placing ArrayLists in ArrayLists.
Here is 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 <> empty after the new ArrayList <>. Java will infer the type (no matter how nested), since java 7 I count. I just wrote them in this example to show what type you are processing at each level to make the example more understandable. You can still write them down to make your code more readable.
Nikhil Pathania
source share