{"id":5287,"date":"2021-03-23T16:57:37","date_gmt":"2021-03-23T16:57:37","guid":{"rendered":"https:\/\/ded9.com\/?p=5287"},"modified":"2025-11-05T09:07:24","modified_gmt":"2025-11-05T09:07:24","slug":"multidimensional-arrays-in-java-in-very-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/multidimensional-arrays-in-java-in-very-simple-language\/","title":{"rendered":"Mastering Multidimensional Arrays in Java: A Beginner&#8217;s Guide"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">In Java, you can define an array of multi-dimensional arrays. Before learning about multi-dimensional arrays.<\/span><span id=\"more-50109\"><\/span><\/p>\n<p>In that tutorial, you learned how to create and use an array of primary data types (such as Double, int, etc.), a string array, and an object array. An array of arrays can also be created. For example,<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">int [] [] a = new int [3] [4];<\/pre>\n<\/div>\n<p>A is a two-dimensional array that holds up to 12 integer elements.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"lazyloaded aligncenter wp-image-50111\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-java-2d-array-jpg.jpeg\" alt=\"Mastering Multidimensional Arrays in Java: A Beginner's Guide\" width=\"399\" height=\"275\" data-ll-status=\"loaded\" \/><\/p>\n<p>Note that the array starts from 0, not 1.<\/p>\n<p>You can also define a three-dimensional array. For example:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">String [] [] [] personalInfo = new String [3] [4] [2];<\/pre>\n<\/div>\n<p>PersonalInfo is a 3D array that holds up to 24 string elements.<\/p>\n<p>In Java, the components of a<a href=\"https:\/\/www.geeksforgeeks.org\/multidimensional-arrays-in-java\/\" target=\"_blank\" rel=\"noopener\"> multi-dimensional array<\/a> are also arrays.<\/p>\n<p>If you are familiar with C++ \/ C, you may feel that multi-dimensional arrays work similarly in Java and C++ \/ C. Well, it is not. In <a href=\"https:\/\/ded9.com\/multidimensional-arrays-in-java-in-very-simple-language\/\">Java<\/a>, rows can have different lengths.<\/p>\n<p>You will notice the difference when initializing.<\/p>\n<h2><span style=\"font-size: 18pt;\">How do you initialize a 2D array in Java?<\/span><\/h2>\n<p>Here is an example of how to initialize a 2D array in Java.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">int [] [] a = {\r\n\r\n{1, 2, 3},\r\n\r\n{4, 5, 6, 9},\r\n\r\n{7},\r\n\r\n};<\/pre>\n<\/div>\n<p>As mentioned, each array component a is an array in itself, and the length of each row is different.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-50112\" title=\"Arrays \" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-2d-array-variable-length-jpg.jpeg\" alt=\"Arrays \" width=\"400\" height=\"282\" data-ll-status=\"loaded\" \/><\/p>\n<h3>Let&#8217;s write a program to prove it.<\/h3>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class MultidimensionalArray {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int [] [] a = {<\/li>\n<li dir=\"ltr\">{1, 2, 3},<\/li>\n<li dir=\"ltr\">{4, 5, 6, 9},<\/li>\n<li dir=\"ltr\">{7},<\/li>\n<li dir=\"ltr\">};<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">System.out.println (\u201cLength of row 1:\u201d + a [0] .length);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cLength of row 2:\u201d + a [1] .length);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cLength of row 3:\u201d + a [2] .length);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h3><strong>Output<\/strong><\/h3>\n<blockquote>\n<p dir=\"ltr\">Length of row 1: 3<\/p>\n<p dir=\"ltr\">Length of row 2: 4<\/p>\n<p dir=\"ltr\">Length of row 3: 1<\/p>\n<\/blockquote>\n<p>Since each component of a multi-dimensional array is also an array ([a [0], a [1, and [a [2 are also arrays), you can use the length property to find the length of each row.<\/p>\n<p><strong>Example: Print all the elements of a 2D array using a loop<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class MultidimensionalArray {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int [] [] a = {<\/li>\n<li dir=\"ltr\">{1, -2, 3},<\/li>\n<li dir=\"ltr\">{-4, -5, 6, 9},<\/li>\n<li dir=\"ltr\">{7},<\/li>\n<li dir=\"ltr\">};<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">for (int i = 0; i &lt;a.length; ++ i) {<\/li>\n<li dir=\"ltr\">for (int j = 0; j &lt;a [i] .length; ++ j) {<\/li>\n<li dir=\"ltr\">System.out.println (a [i] [j]);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h3><strong>Output<\/strong><\/h3>\n<p>It may be best to use the for\u2026 each loop at all times.\u00a0You can do the same with the for\u2026 each loop:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class MultidimensionalArray {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int [] [] a = {<\/li>\n<li dir=\"ltr\">{1, -2, 3},<\/li>\n<li dir=\"ltr\">{-4, -5, 6, 9},<\/li>\n<li dir=\"ltr\">{7},<\/li>\n<li dir=\"ltr\">};<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">for (int [] innerArray: a))<\/li>\n<li dir=\"ltr\">for (int data: innerArray) {<\/li>\n<li dir=\"ltr\">System.out.println (data);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h3><strong>Output<\/strong><\/h3>\n<blockquote>\n<p dir=\"ltr\">1<\/p>\n<p dir=\"ltr\">-2<\/p>\n<p dir=\"ltr\">3<\/p>\n<p dir=\"ltr\">-4<\/p>\n<p dir=\"ltr\">-5<\/p>\n<p dir=\"ltr\">6<\/p>\n<p dir=\"ltr\">9<\/p>\n<p dir=\"ltr\">7<\/p>\n<\/blockquote>\n<h2><span style=\"font-size: 18pt;\"><strong>How do you initialize a 3D array in Java?<\/strong><\/span><\/h2>\n<p>You can set the 3D array in the same way as a 2D array. Consider the following example:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">\/\/ test is a 3d array\r\n\r\nint [] [] [] test = =\r\n\r\n{\r\n\r\n{1, -2, 3},\r\n\r\n{2, 3, 4}\r\n\r\n},\r\n\r\n{\r\n\r\n{-4, -5, 6, 9},\r\n\r\n{1},\r\n\r\n{2, 3}\r\n\r\n}\r\n\r\n};<\/pre>\n<\/div>\n<p>In essence, a three-dimensional array is an array of two-dimensional arrays.<\/p>\n<p>Similar to 2D arrays, rows of 3D arrays can have different lengths.<\/p>\n<p><strong>Example: A program for printing 3D array elements using loops<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class ThreeArray {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">\/\/ test is a 3d array<\/li>\n<li dir=\"ltr\">int [] [] [] test = =<\/li>\n<li dir=\"ltr\">{<\/li>\n<li dir=\"ltr\">{1, -2, 3},<\/li>\n<li dir=\"ltr\">{2, 3, 4}<\/li>\n<li dir=\"ltr\">},<\/li>\n<li dir=\"ltr\">{<\/li>\n<li dir=\"ltr\">{-4, -5, 6, 9},<\/li>\n<li dir=\"ltr\">{1},<\/li>\n<li dir=\"ltr\">{2, 3}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">};<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">\/\/ for..each loop to iterate through elements of 3d array<\/li>\n<li dir=\"ltr\">for (int [] [] array2D: test) {<\/li>\n<li dir=\"ltr\">for (int [] array1D: array2D) {<\/li>\n<li dir=\"ltr\">for (int item: array1D) {<\/li>\n<li dir=\"ltr\">System.out.println (item);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4><strong>Output<\/strong><\/h4>\n<blockquote>\n<p dir=\"ltr\">1<\/p>\n<p dir=\"ltr\">-2<\/p>\n<p dir=\"ltr\">3<\/p>\n<p dir=\"ltr\">2<\/p>\n<p dir=\"ltr\">3<\/p>\n<p dir=\"ltr\">4<\/p>\n<p dir=\"ltr\">-4<\/p>\n<p dir=\"ltr\">-5<\/p>\n<p dir=\"ltr\">6<\/p>\n<p dir=\"ltr\">9<\/p>\n<p dir=\"ltr\">1<\/p>\n<p dir=\"ltr\">2<\/p>\n<p dir=\"ltr\">3<\/p>\n<\/blockquote>\n<h2>Loop Through a Multi-Dimensional Array<\/h2>\n<p>You can also use a\u00a0<code class=\"w3-codespan\">for loop<\/code>\u00a0inside another\u00a0<code class=\"w3-codespan\">for loop<\/code>\u00a0to get the elements of a two-dimensional array (we still have to point to the two indexes):<\/p>\n<div class=\"w3-example\">\n<h3>Example<\/h3>\n<pre class=\"w3-white language-java\" tabindex=\"0\"><code class=\"language-java\"><span class=\"token keyword keyword-int\">int<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> myNumbers <span class=\"token operator\">=<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">4<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">6<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">7<\/span><span class=\"token punctuation\">}<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span>\r\n<span class=\"token keyword keyword-for\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword keyword-int\">int<\/span> i <span class=\"token operator\">=<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> myNumbers<span class=\"token punctuation\">.<\/span>length<span class=\"token punctuation\">;<\/span> <span class=\"token operator\">++<\/span>i<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n  <span class=\"token keyword keyword-for\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword keyword-int\">int<\/span> j <span class=\"token operator\">=<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> j <span class=\"token operator\">&lt;<\/span> myNumbers<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span>length<span class=\"token punctuation\">;<\/span> <span class=\"token operator\">++<\/span>j<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n    <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">.<\/span><span class=\"token function\">println<\/span><span class=\"token punctuation\">(<\/span>myNumbers<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">[<\/span>j<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>\r\n  <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">\r\n}<\/span><\/code><\/pre>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is a multidimensional array in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It's an array of arrays, allowing you to store data in a table-like format with rows and columns.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do I declare a 2D array in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use syntax like int[][] arrayName; to declare a 2D array.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can Java arrays have rows of different lengths?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, Java supports jagged arrays where each row can have a different number of columns.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Java, you can define an array of multi-dimensional arrays. Before learning about multi-dimensional arrays. In that tutorial, you learned how to create and use an array of primary data types (such as Double, int, etc.), a string array, and an object array. An array of arrays can also be created. For example, int [] [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5288,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=5287"}],"version-history":[{"count":7,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5287\/revisions"}],"predecessor-version":[{"id":265003,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5287\/revisions\/265003"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/5288"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=5287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=5287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=5287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}