Multi-Dimensional Arrays

Home / Core Java / Multi-Dimensional Arrays

Multi-Dimensional Arrays


Multi-Dimensional Arrays : 

  • Multi-dimensional arrays in Java allow you to create arrays of arrays, forming a structure where each element is itself an array.
  • They provide a way to represent tables or matrices with rows and columns.
  • In Java, you can create two-dimensional arrays, three-dimensional arrays, or even higher dimensions as needed.

Here are some common uses of Multi-Dimensional Arrays in  real-life scenarios : 

  • Image Processing
  • Matrices and Linear Algebra
  • Games and Graphics
  • Database Systems
  • AI and Machine Learning


Two-Dimensional Array :

A two-dimensional array in Java is an array of arrays. It's like a table with rows and columns.

Declaration and Initialization:

  • dataType represents the type of elements the array will hold.
  • rows indicate the number of arrays or "rows" in the 2D array.
  • columns indicate the number of elements in each "row."


Example:


Accessing Elements:

Accessing elements in a two-dimensional array involves using two indices: one for the row and another for the column.



Three-Dimensional Arrays and Beyond:

You can extend this concept to three-dimensional arrays and beyond, creating arrays of arrays of arrays. For instance, a three-dimensional array is like a cube, with layers of rows and columns.

Declaration and Initialization of a 3D array:



Example of a 3D array:

The concept extends further for higher dimensions, creating arrays of arrays of arrays, and so on.