{"id":1176,"date":"2022-02-24T19:46:57","date_gmt":"2022-02-24T19:46:57","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1176"},"modified":"2022-02-24T19:46:57","modified_gmt":"2022-02-24T19:46:57","slug":"multidimensional-arrays","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/multidimensional-arrays\/","title":{"rendered":"Multidimensional Arrays"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java multidimensional array using 2-dimensional arrays and 3-dimensional arrays with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">Before we learn about the multidimensional array, make sure you know about\u00a0Java array.<\/p>\n\n\n\n<p>A multidimensional array is an array of arrays. Each element of a multidimensional array is an array itself. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int&#91;]&#91;] a = new int&#91;3]&#91;4];<\/code><\/pre>\n\n\n\n<p>Here, we have created a multidimensional array named&nbsp;<var>a<\/var>. It is a 2-dimensional array, that can hold a maximum of 12 elements,<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-2d-array.jpg\" alt=\"2-dimensional array in Java\" title=\"2-dimensional Array\"\/><figcaption>2-dimensional Array<\/figcaption><\/figure>\n\n\n\n<p>Remember, Java uses zero-based indexing, that is, indexing of arrays in Java starts with 0 and not 1.<\/p>\n\n\n\n<p>Let&#8217;s take another example of the multidimensional array. This time we will be creating a 3-dimensional array. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String&#91;]&#91;]&#91;] data = new String&#91;3]&#91;4]&#91;2];<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>data<\/var>&nbsp;is a 3d array that can hold a maximum of 24 (3*4*2) elements of type&nbsp;<code>String<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2d\">How to initialize a 2d array in Java?<\/h2>\n\n\n\n<p>Here is how we can initialize a 2-dimensional array in Java.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int&#91;]&#91;] a = {\n      {1, 2, 3}, \n      {4, 5, 6, 9}, \n      {7}, \n};<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/2d-array-variable-length.jpg\" alt=\"2d array example in Java with variable length\" title=\"Initialization of 2-dimensional Array\"\/><figcaption>Initialization of 2-dimensional Array<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Example: 2-dimensional Array<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class MultidimensionalArray {\n    public static void main(String&#91;] args) {\n\n        \/\/ create a 2d array\n        int&#91;]&#91;] a = {\n            {1, 2, 3}, \n            {4, 5, 6, 9}, \n            {7}, \n        };\n      \n        \/\/ calculate the length of each row\n        System.out.println(\"Length of row 1: \" + a&#91;0].length);\n        System.out.println(\"Length of row 2: \" + a&#91;1].length);\n        System.out.println(\"Length of row 3: \" + a&#91;2].length);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Length of row 1: 3\nLength of row 2: 4\nLength of row 3: 1<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we are creating a multidimensional array named&nbsp;<var>a<\/var>. Since each component of a multidimensional array is also an array (<code>a[0]<\/code>,&nbsp;<code>a[1]<\/code>&nbsp;and&nbsp;<code>a[2]<\/code>&nbsp;are also arrays).<\/p>\n\n\n\n<p>Here, we are using the&nbsp;<code>length<\/code>&nbsp;attribute to calculate the length of each row.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2d-example\">Example: Print all elements of 2d array Using Loop<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class MultidimensionalArray {\n    public static void main(String&#91;] args) {\n\n        int&#91;]&#91;] a = {\n            {1, -2, 3}, \n            {-4, -5, 6, 9}, \n            {7}, \n        };\n      \n        for (int i = 0; i &lt; a.length; ++i) {\n            for(int j = 0; j &lt; a&#91;i].length; ++j) {\n                System.out.println(a&#91;i]&#91;j]);\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>1\n-2\n3\n-4\n-5\n6\n9\n7<\/samp><\/code><\/pre>\n\n\n\n<p>We can also use the&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/enhanced-for-loop\">for&#8230;each loop<\/a>&nbsp;to access elements of the multidimensional array. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MultidimensionalArray {\n    public static void main(String&#91;] args) {\n\n        \/\/ create a 2d array\n        int&#91;]&#91;] a = {\n            {1, -2, 3}, \n            {-4, -5, 6, 9}, \n            {7}, \n        };\n      \n        \/\/ first for...each loop access the individual array\n        \/\/ inside the 2d array\n        for (int&#91;] innerArray: a) {\n            \/\/ second for...each loop access each element inside the row\n            for(int data: innerArray) {\n                System.out.println(data);\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n-2\n3\n-4\n-5\n6\n9\n7<\/code><\/pre>\n\n\n\n<p>In the above example, we are have created a 2d array named&nbsp;<var>a<\/var>. We then used&nbsp;<code>for<\/code>&nbsp;loop and&nbsp;<code>for...each<\/code>&nbsp;loop to access each element of the array.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3d-array\">How to initialize a 3d array in Java?<\/h2>\n\n\n\n<p>Let&#8217;s see how we can use a 3d array in Java. We can initialize a 3d array similar to the 2d array. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ test is a 3d array\nint&#91;]&#91;]&#91;] test = {\n        {\n          {1, -2, 3}, \n          {2, 3, 4}\n        }, \n        { \n          {-4, -5, 6, 9}, \n          {1}, \n          {2, 3}\n        } \n};<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3d-example\">Example: 3-dimensional Array<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class ThreeArray {\n    public static void main(String&#91;] args) {\n\n        \/\/ create a 3d array\n        int&#91;]&#91;]&#91;] test = {\n            {\n              {1, -2, 3}, \n              {2, 3, 4}\n            }, \n            { \n              {-4, -5, 6, 9}, \n              {1}, \n              {2, 3}\n            } \n        };\n\n        \/\/ for..each loop to iterate through elements of 3d array\n        for (int&#91;]&#91;] array2D: test) {\n            for (int&#91;] array1D: array2D) {\n                for(int item: array1D) {\n                    System.out.println(item);\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>1\n-2\n3\n2\n3\n4\n-4\n-5\n6\n9\n1\n2\n3<\/samp><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java multidimensional array using 2-dimensional arrays and 3-dimensional arrays with the help of examples. Before we learn about the multidimensional array, make sure you know about\u00a0Java array. A multidimensional array is an array of arrays. Each element of a multidimensional array is an array itself. For example, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[427],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1176"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1176"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1176\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}