Java Multidimensional Array

Java Multi dimensional Array

What is Java Multidimensional Array? 

In Java multidimensional array has more than one dimension. It can be thought of as an array of arrays.

A two-dimensional array is a common example of a multidimensional array, where each element of the array contains an array of elements.

To understand the Java Multidimensional Array, Read the Complete Article.

Types of Multi dimensional Array

In Java, arrays can have one or more dimensions, allowing you to store and access multiple values using a single variable. Here are examples of one-dimensional, two-dimensional, and three-dimensional arrays in Java:

int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[2]); // Output: 3
int[][] matrix = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(matrix[1][0]); // Output: 3
int[][][] cube = {
    {{1, 2}, {3, 4}},
    {{5, 6}, {7, 8}},
    {{9, 10}, {11, 12}}
};
System.out.println(cube[2][1][0]); // Output: 11
Multi Threading In Java

Let’s look at the Java Multi Dimensional Array to perform certain operations.

Example 1: Java program to create one dimensional Array:

Run
//OneDimensionalArrayExample
public class Main {
    public static void main(String[] args) {
        // Create an array of integers with length 5
        int[] numbers = new int[5];

        // Assign values to the array
        numbers[0] = 1;
        numbers[1] = 3;
        numbers[2] = 5;
        numbers[3] = 7;
        numbers[4] = 9;

        // Print the values in the array
        for (int i = 0; i < numbers.length; i++) {
            System.out.println("Element at index " + i + " is " + numbers[i]);
        }
    }
}

Output

Element at index 0 is 1
Element at index 1 is 3
Element at index 2 is 5
Element at index 3 is 7
Element at index 4 is 9

Example 2 : Java Program to create two dimentional array

Run
public class Main
{
  

public static void main (String[]args)
  {
    
      // Create a two-dimensional array with 3 rows and 5 columns
    int[][] arr =
      { {50, 60, 65, 70, 75}, {52, 57, 62, 67, 72}, {78, 83, 88, 93, 97} };
    
 
      // Print the values of the array
      for (int i = 0; i < arr.length; i++)
      {
	
for (int j = 0; j < arr[i].length; j++)
	  {
	    
System.out.print (arr[i][j] + " ");
	  
} 
System.out.println ();

} 
} 
} 

Output

50 60 65 70 75 
52 57 62 67 72 
78 83 88 93 97 
Java Multi dimensional Array

Example 3 : Java Program to create three dimentional array:

Run
public class Main
{
  

public static void main (String[]args)
  {
    
      // Create a three-dimensional array with 2 layers, 3 rows, and 4 columns
    int[][][] arr = { 
	{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}, 
      {{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}} 
    };
    
 
      // Print the values of the array
      for (int i = 0; i < arr.length; i++)
      {
	
for (int j = 0; j < arr[i].length; j++)
	  {
	    
for (int k = 0; k < arr[i][j].length; k++)
	      {
		
System.out.print (arr[i][j][k] + " ");
	      
} 
System.out.println ();
	  
} 
System.out.println ();

} 
} 
} 

Output

1 2 3 4 
5 6 7 8 
9 10 11 12 

13 14 15 16 
17 18 19 20 
21 22 23 24 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription