Multidimensional Arrays and Their Traversal in Java
Topic Overview
Welcome to today's session on "Multidimensional Arrays and Their Traversal in Java". Multidimensional arrays are types of arrays that store arrays at each index instead of single elements. Picture it as an 'apartment building' with floors (the outer array) and apartments on each floor (the inner array). Our goal today is to strengthen your foundational knowledge of these 'apartment buildings' and how to handle them effectively in Java.
Creating Multidimensional Arrays
To construct a multidimensional array in Java, we use arrays of arrays. Here are examples to demonstrate how to create and work with 2D static arrays.
Indexing in Multidimensional Arrays
All indices in Java arrays are 0-based. Let's say you want to visit an apartment on the second floor (index 1) and bring a package to the first unit (index 0) in this building. Here's how you can do it:
We visited the element 4 in the array by its position. The number [1] refers to the second inner array, and [0] refers to the first element of that array.
Traversing Multidimensional Arrays
We can visit every floor (outer array) and every apartment on each floor (inner array) by using nested loops.
