2d array list in java code example
Example 1: 2d arraylist in java
int vertexCount = 3;
ArrayList<ArrayList<Integer>> graph = new ArrayList<>(vertexCount);
for(int i=0; i < vertexCount; i++) {
graph.add(new ArrayList());
}
Example 2: 2d array java
int[][]arr= new int [filas][columnas];
arr.length=filas;
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
System.out.println("Length of row 1: " + a[0].length);
System.out.println("Length of row 2: " + a[1].length);
System.out.println("Length of row 3: " + a[2].length);
}
Example 3: 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>());
}