intialize 2d array java code example
Example 1: how to print a 2d array in java
for (int row = 0; row < arr.length; row++)
{
for (int col = 0; col < arr[row].length; col++)
{
System.out.printf("%5d", arr[row][col]);
}
System.out.println();
}
Example 2: Multidimensional array in java
public class TwoDimensionalStringArray
{
public static void main(String[] args)
{
String[][] animals = {
{ "Lion", "Tiger", "Cheetah" },
{ "Deer", "Jackal", "Bear" },
{ "Hyena", "Fox", "Elephant" } };
for(int a = 0; a < animals.length; a++)
{
System.out.print(animals[a][0] + " ");
for(int b = 1; b < animals[a].length; b++)
{
System.out.print(animals[a][b] + " ");
}
System.out.println();
}
}
}
Example 3: 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 4: How to create a 2d array in java
int[][] arr = new int[m][n];