|
| 1 | +--- |
| 2 | +title: Java Multidimensional Arrays |
| 3 | +description: In this tutorial, we will learn how to use the Java continue statement to skip the current iteration of a loop. |
| 4 | +--- |
| 5 | + |
| 6 | +## Java Multidimensional Arrays |
| 7 | +Before we learn about the multidimensional array, make sure you know about Java array. |
| 8 | + |
| 9 | +A multidimensional array is an array of arrays. Each element of a multidimensional array is an array itself. For example, |
| 10 | + |
| 11 | +```java |
| 12 | +int[][] a = new int[3][4]; |
| 13 | +``` |
| 14 | +Here, we have created a multidimensional array named a. It is a 2-dimensional array, that can hold a maximum of 12 elements, |
| 15 | + |
| 16 | + |
| 17 | +2-dimensional Array |
| 18 | + |
| 19 | +Remember, Java uses zero-based indexing, that is, indexing of arrays in Java starts with 0 and not 1. |
| 20 | + |
| 21 | +Let's take another example of the multidimensional array. This time we will be creating a 3-dimensional array. For example, |
| 22 | + |
| 23 | +```java |
| 24 | +String[][][] data = new String[3][4][2]; |
| 25 | +``` |
| 26 | +Here, data is a 3d array that can hold a maximum of 24 (3*4*2) elements of type [String](/docs/string). |
| 27 | + |
| 28 | +### How to initialize a 2d array in Java? |
| 29 | +Here is how we can initialize a 2-dimensional array in Java. |
| 30 | + |
| 31 | +```java |
| 32 | +int[][] a = { |
| 33 | + {1, 2, 3}, |
| 34 | + {4, 5, 6, 9}, |
| 35 | + {7}, |
| 36 | +}; |
| 37 | +``` |
| 38 | +As we can see, each element of the multidimensional array is an array itself. And also, unlike C/C++, each row of the multidimensional array in Java can be of different lengths. |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +Initialization of 2-dimensional Array |
| 43 | + |
| 44 | +### Example: 2-dimensional Array |
| 45 | +```java |
| 46 | +class MultidimensionalArray { |
| 47 | + public static void main(String[] args) { |
| 48 | + |
| 49 | + // create a 2d array |
| 50 | + int[][] a = { |
| 51 | + {1, 2, 3}, |
| 52 | + {4, 5, 6, 9}, |
| 53 | + {7}, |
| 54 | + }; |
| 55 | + |
| 56 | + // calculate the length of each row |
| 57 | + System.out.println("Length of row 1: " + a[0].length); |
| 58 | + System.out.println("Length of row 2: " + a[1].length); |
| 59 | + System.out.println("Length of row 3: " + a[2].length); |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | +#### Output: |
| 64 | + |
| 65 | +```plaintext |
| 66 | +Length of row 1: 3 |
| 67 | +Length of row 2: 4 |
| 68 | +Length of row 3: 1 |
| 69 | +``` |
| 70 | +In the above example, we are creating a multidimensional array named a. Since each component of a multidimensional array is also an array (`a[0]`,`a[1]` and `a[2]` are also arrays). |
| 71 | + |
| 72 | +Here, we are using the `length` attribute to calculate the length of each row. |
| 73 | + |
| 74 | +### Example: Print all elements of 2d array Using Loop |
| 75 | + |
| 76 | +```java |
| 77 | +class MultidimensionalArray { |
| 78 | + public static void main(String[] args) { |
| 79 | + |
| 80 | + int[][] a = { |
| 81 | + {1, -2, 3}, |
| 82 | + {-4, -5, 6, 9}, |
| 83 | + {7}, |
| 84 | + }; |
| 85 | + |
| 86 | + for (int i = 0; i < a.length; ++i) { |
| 87 | + for(int j = 0; j < a[i].length; ++j) { |
| 88 | + System.out.println(a[i][j]); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | +#### Output: |
| 95 | + |
| 96 | +```plaintext |
| 97 | +1 |
| 98 | +-2 |
| 99 | +3 |
| 100 | +-4 |
| 101 | +-5 |
| 102 | +6 |
| 103 | +9 |
| 104 | +7 |
| 105 | +``` |
| 106 | +We can also use the [for...each loop](/docs/enhanced-for-loop) to access elements of the multidimensional array. For example, |
| 107 | + |
| 108 | +```java |
| 109 | +class MultidimensionalArray { |
| 110 | + public static void main(String[] args) { |
| 111 | + |
| 112 | + // create a 2d array |
| 113 | + int[][] a = { |
| 114 | + {1, -2, 3}, |
| 115 | + {-4, -5, 6, 9}, |
| 116 | + {7}, |
| 117 | + }; |
| 118 | + |
| 119 | + // first for...each loop access the individual array |
| 120 | + // inside the 2d array |
| 121 | + for (int[] innerArray: a) { |
| 122 | + // second for...each loop access each element inside the row |
| 123 | + for(int data: innerArray) { |
| 124 | + System.out.println(data); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | +Output: |
| 131 | + |
| 132 | +```plaintext |
| 133 | +1 |
| 134 | +-2 |
| 135 | +3 |
| 136 | +-4 |
| 137 | +-5 |
| 138 | +6 |
| 139 | +9 |
| 140 | +7 |
| 141 | +``` |
| 142 | +In the above example, we are have created a 2d array named `a`. We then used `for` loop and `for...each` loop to access each element of the array. |
| 143 | + |
| 144 | +### How to initialize a 3d array in Java? |
| 145 | +Let's see how we can use a 3d array in Java. We can initialize a 3d array similar to the 2d array. For example, |
| 146 | + |
| 147 | +```java |
| 148 | +// test is a 3d array |
| 149 | +int[][][] test = { |
| 150 | + { |
| 151 | + {1, -2, 3}, |
| 152 | + {2, 3, 4} |
| 153 | + }, |
| 154 | + { |
| 155 | + {-4, -5, 6, 9}, |
| 156 | + {1}, |
| 157 | + {2, 3} |
| 158 | + } |
| 159 | +}; |
| 160 | +``` |
| 161 | +Basically, a 3d array is an array of 2d arrays. The rows of a 3d array can also vary in length just like in a 2d array. |
| 162 | + |
| 163 | +### Example: 3-dimensional Array |
| 164 | +```java |
| 165 | +class ThreeArray { |
| 166 | + public static void main(String[] args) { |
| 167 | + |
| 168 | + // create a 3d array |
| 169 | + int[][][] test = { |
| 170 | + { |
| 171 | + {1, -2, 3}, |
| 172 | + {2, 3, 4} |
| 173 | + }, |
| 174 | + { |
| 175 | + {-4, -5, 6, 9}, |
| 176 | + {1}, |
| 177 | + {2, 3} |
| 178 | + } |
| 179 | + }; |
| 180 | + |
| 181 | + // for..each loop to iterate through elements of 3d array |
| 182 | + for (int[][] array2D: test) { |
| 183 | + for (int[] array1D: array2D) { |
| 184 | + for(int item: array1D) { |
| 185 | + System.out.println(item); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | +``` |
| 192 | +#### Output: |
| 193 | + |
| 194 | +```plaintext |
| 195 | +1 |
| 196 | +-2 |
| 197 | +3 |
| 198 | +2 |
| 199 | +3 |
| 200 | +4 |
| 201 | +-4 |
| 202 | +-5 |
| 203 | +6 |
| 204 | +9 |
| 205 | +1 |
| 206 | +2 |
| 207 | +3 |
| 208 | +``` |
0 commit comments