Java
Java
Output
arr[0][0] = 1
Syntax for Multi-Dimensional Array
data_type[1st dimension][2nd dimension][]..[Nth dimension]
array_name = new data_type[size1][size2]….[sizeN];
Parameters:
data_type: Type of data to be stored in the array. For example: int,
char, etc.
dimension: The dimension of the array created. For example: 1D, 2D,
etc.
array_name: Name of the array
size1, size2, …, sizeN: Sizes of the dimensions respectively.
Examples:
// Two dimensional array:
int[][] twoD_arr = new int[10][20];
// Three dimensional array:
int[][][] threeD_arr = new int[10][20][30];
Size of Multidimensional Arrays: The total number of elements that
can be stored in a multidimensional array can be calculated by
multiplying the size of all the dimensions.
For example: array int[][][] x = new int[5][10][20] can store a total
of (5*10*20) = 1000 elements.
Example 1: We can add the values directly to the array while declaring
the array.
// Java Program to demonstrate the use of
// Two Dimensional Array
import java.io.*;
class Main {
public static void main(String[] args){
// Array Intialised and Assigned
int[][] arr = { { 1, 2 }, { 3, 4 } };
Output
1 2
3 4
Example 2: Here we can update the values while executing works both
ways can be accepted by user or by some variable.
// Java Program to demonstrate the use of
// Two Dimensional Array
public class TwoDArray {
public static void main(String[] args) {
int it = 1;