2d arraylists java code example

Example 1: 2d arraylist in java

int vertexCount = 3;
ArrayList<ArrayList<Integer>> graph = new ArrayList<>(vertexCount);
//Next, we'll initialize each element of ArrayList with another ArrayList:

for(int i=0; i < vertexCount; i++) {
    graph.add(new ArrayList());
}

Example 2: creating a 2d arraylist in java

List<List<String>> listOfLists = new ArrayList<List<String>>(size); 
for(int i=0; i<size; i++) {
	listOfLists.add(new ArrayList<String>());
}

Tags:

Java Example