1// two dimensional string array in java example
2public class TwoDimensionalStringArray
3{
4 public static void main(String[] args)
5 {
6 String[][] animals = {
7 { "Lion", "Tiger", "Cheetah" },
8 { "Deer", "Jackal", "Bear" },
9 { "Hyena", "Fox", "Elephant" } };
10 for(int a = 0; a < animals.length; a++)
11 {
12 System.out.print(animals[a][0] + " ");
13 for(int b = 1; b < animals[a].length; b++)
14 {
15 System.out.print(animals[a][b] + " ");
16 }
17 System.out.println();
18 }
19 }
20}
1// how to fill a 2d array java
2public class Fill2dArrayExample
3{
4 public static void main(String[] args)
5 {
6 int[][] arrNumbers = new int[3][3];
7 for(int a = 0; a < 3; a++)
8 {
9 for(int b = 0; b < 3; b++)
10 {
11 arrNumbers[a][b] = (int) (Math.random() * 6) ;
12 }
13 }
14 for(int x = 0; x < 3; x++)
15 {
16 for(int y = 0; y < 3; y++)
17 {
18 System.out.print(arrNumbers[x][y] + " ");
19 }
20 System.out.println();
21 }
22 }
23}
1// two dimensional array in java example program
2public class SimpleTwodimensionalArray
3{
4 public static void main(String[] args)
5 {
6 // declare and initialize two dimensional array
7 int[][] arrnumbers = {{2, 4},{6, 8},{10, 12}};
8 System.out.println("Two dimensional array in java: ");
9 for(int a = 0; a < 3; a++)
10 {
11 for(int b = 0; b < 2; b++)
12 {
13 System.out.println(arrnumbers[a][b]);
14 }
15 }
16 }
17}
1// create dynamic two dimensional array in java
2import java.util.ArrayList;
3import java.util.List;
4public class Dynamic2dArray
5{
6 public static void main(String[] args)
7 {
8 List<int[]> li = new ArrayList<>();
9 li.add(new int[]{2,4,6});
10 li.add(new int[]{3,5});
11 li.add(new int[]{1});
12 // element at row 0, column 0
13 System.out.println("Element at [0][0]: " + li.get(0)[1]);
14 // get element at row : 1, column : 1
15 System.out.println("Element at [1][1]: " + li.get(1)[1]);
16 }
17}
1// java two dimensional array length example
2public class TwoDimensionalArrayLength
3{
4 public static void main(String[] args)
5 {
6 int[][] arrNumber = {
7 { 1, 3, 5 },
8 { 7, 9, 2 },
9 { 1, 2, 3, 4, 5 } };
10 System.out.println("Java two dimensional array length: " + arrNumber.length);
11 }
12}
1// Syntax Multidimensional array in java
2datatype[1][2]...[nth dimension] arrayname = new datatype[1][2]...[nth size];