Accessing Elements of Two-Dimensional Arrays
Elements in two-dimensional arrays are commonly referred by x[i][j] where ‘i’ is
the row number and ‘j’ is the column number.
Note: In arrays if size of array is N. Its index will be from 0 to N-1. Therefore, for
row_index 2, actual row number is 2+1 = 3.
Example:
// Java Program to demonstrate Accessing
// Elements of Two-Dimensional Arrays
import java.io.*;
class Main {
public static void main(String[] args){
int[][] arr = { { 1, 2 },
{ 3, 4 } };
// Accessing Element at index
// row=1 and column=1
System.out.println("a[1][1] : " + arr[1][1]);
}
}
Output
a[1][1] : 4
Two Dimensional Array with User input
Follow the Steps mentioned below to create a Two Dimensional Array with User
input:
This code prompts the user to enter the number of rows and columns for the
Two-dimensional array. The Scanner class is used to read the user input. Then
it creates a Two-dimensional array of integers with the specified number of rows
and columns, and assigns each element of the array with i*j.
If you want to create a multidimensional array with more than two dimensions,
you can use the same approach of creating an array of arrays.
Example:
// Java Program for Creating two
// Dimensional array with user Inputs
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
// Taking Number of Rows and Columns from User
System.out.print("Enter number of rows: ");
int row = scn.nextInt();
System.out.print("Enter number of columns: ");
int col = scn.nextInt();
int[][] arr= new int[row][col];
// Operating on Two Dimensional Array
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
arr[i][j]= (i + 1) * (j + 1);
}
}
// Printing Elements of Arrays
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(arr[i][j]+ " ");
}
System.out.println();
}
scn.close();
}
}
Input:
Enter number of rows: 3
Enter number of columns: 3
Output:
1 2 3
2 4 6
3 6 9
Three – Dimensional Array (3D-Array)
3D-Array is a complex form of a multidimensional array. A 3D-array can be seen as
an array of 2D array for easier understanding.
Representation of 3D Array in Tabular Format
A three-dimensional array can be seen as a table of arrays with ‘x’ rows and ‘y’
columns where the row number ranges from 0 to (x-1) and column number ranges
from 0 to (y-1). A three – dimensional array with 3 array containing 3 rows and 3
columns is shown below:
Example 1:
// Java Program to Demonstrate
// Three Dimensional Array
import java.io.*;
class Main {
public static void main(String[] args){
// Array Created and Initialized
int[][][] arr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
// Defining the x,y,z in Multi
// Dimensional Array
int n = arr.length;
int m = arr[0].length;
int o = arr[0][0].length;
// Printing the Array
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++){
for(int k=0; k < o; k++)
System.out.print(arr[i][j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}
Output
arr[0][0][0] = 1
arr[0][0][1] = 2
arr[0][1][0] = 3
arr[0][1][1] = 4
arr[1][0][0] = 5
arr[1][0][1] = 6
arr[1][1][0] = 7
arr[1][1][1] = 8
Example 2:
// Java Program to Implement
// Three Dimensional Array
import java.io.*;
class Main {
public static void main(String[] args){
int[][][] arr = new int[2][2][2];
// Three Dimensional x,y,z dimension
int n=arr.length;
int m=arr[0].length;
int o=arr[0][0].length;
int it=1;
// Assigning the values to array
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for(int k=0; k < o; k++){
arr[i][j][k] = it;
it++;
}
}
}
// Printing the Array
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++){
for(int k=0; k < o; k++)
System.out.print(arr[i][j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}