Array

Three Dimension Array in Java Language

Three Dimension Array in Java Language

In this tutorial, we can get to know about Three Dimension Array in Java Language.

In the Java programming language, an array is a fixed sequential collection of an element of the same data types. An array can be used to represent a list of numbers(int) or names (string) or other data type of similar elements. It is one of the ways to group similar type of data within the single variable name.

Three types of arrays in Java programming language

1.  One dimensional array

2. Two-dimensional array

3. Three-dimensional array

Three-dimensional array is the collection of two-dimensional arrays in Java programming language. Three-dimensional array is also called the multidimensional array.

Three D Array in Java

Declaration of the three dimensional Array

Syntax

date_type[][][] arrayname;----1   or

date_type [][][]arrayname;----2    or

date_type arrayname [][][] ;----3

we can choose any one method

 

Explanation of Three dimensional Array declaration

int [][][]array;
Explanation of 3d array declaration

Forming of three dimension array

Syntax

data_type[][][]array_name=new data_type;

Example

int array[][][]=new int[2][3][3];
Explanation

How to find the index of 3 D Array

How to find index 3d Array

Above diagram describes how to find index of three dimension array. Every array has two index 0 to 1

so we can find index follows

array_name[0][0][0]  //first index

array_name[0][0][1]  //second index

array_name[0][1][0]  //third index

array_name[0][1][1]  //forth index

array_name[1][0][0]  //fifth index

array_name[1][0][1]  //sixthindex

array_name[1][1][0]  //seven index

array_name[1][1][1]  //eight index

 

How to initialize a 3d array in Java

Method 1

Syntax

array_name[index_1][index_2][index_3]=value;

Example

arr[0][0][0]=45;       //initialize first elements of 3 d array

we can initialize every index, like this

 

Method 2

int[][][] arr{
                        {
                             {34,67,43},
                               {576,697,423},
                                {576,697,423}
                               },
                         {
                             {39,47,33},
                              {376,987,453},
                                {57,69,42}
                               },

program 1

class threedarrayex{
public static void main(String args[])
{
int[][][]marks; //declaration of array
marks=new int[2][2][2];//initiation of array
//initiate elements
marks[0][0][0]=25;
marks[0][0][1]=15;
marks[0][1][0]=20;
marks[0][1][1]=05;
marks[1][0][0]=10;
marks[1][0][1]=13;
marks[1][1][0]=12;
marks[1][1][1]=30;
//Display elements from array
System.out.println(marks[0][0][0]);
System.out.println(marks[0][0][1]);
System.out.println(marks[0][1][0]);
System.out.println(marks[0][1][1]);
System.out.println(marks[1][0][0]);
System.out.println(marks[1][0][1]);
System.out.println(marks[1][1][0]);
System.out.println(marks[1][1][1]);
}
}

 

When the above code is compiled and executed, it will produce the following results

25
15
20
5
10
13
12
30

 

Display the element of the array using nested for loop in Java

Program 1

class threedarrayex1{
public static void main(String args[])
{
int[][][]marks; //declaration of array
marks=new int[2][2][2];//initiation of array
//initiate elements
marks[0][0][0]=25;
marks[0][0][1]=15;
marks[0][1][0]=20;
marks[0][1][1]=05;
marks[1][0][0]=10;
marks[1][0][1]=13;
marks[1][1][0]=12;
marks[1][1][1]=30;
//Display elements from array using for loop
for(int i=0; i<=1; i++){
    for(int j=0; j<=1; j++){
        for(int k=0; k<=1; k++){
    System.out.println(marks[i][j][k]);
}
    
}
}
}
}

 

When the above code is compiled and executed, it will produce the following results

25
15
20
5
10
13
12
30

 

program 2

class threedim_array{
public static void main(String args[]){
int[][][] arr={
                {
                {34,67,43},
                {576,697,423},
                {576,587,90}
                               },

                {
                {39,47,33},
                {376,987,453},
                {57,69,42}
                         },
                         };
                         
        for(int i=0; i<=1; i++){
           for(int j=0; j<=2; j++){
              for(int k=0; k<=2; k++){
           System.out.println(arr[i][j][k]);
        }
        }
        }
        }     
                         
    }

 

When the above code is compiled and executed, it will produce the following results

34
67
43
576
697
423
576
587
90
39
47
33
376
987
453
57
69
42

 

Display the element of the array using Enhanced for loop in Java

Program 1

class threedarrayex2{
public static void main(String args[])
{
int[][][]marks; //declaration of array
marks=new int[2][2][2];//initiation of array
//initiate elements
marks[0][0][0]=255;
marks[0][0][1]=145;
marks[0][1][0]=230;
marks[0][1][1]=205;
marks[1][0][0]=130;
marks[1][0][1]=143;
marks[1][1][0]=152;
marks[1][1][1]=360;
//Display elements from array using for loop
for(int i[][]:marks){
    for(int j[]:i){
        for(int k:j){
    System.out.println(k);
}
    
}
}
}
}

 

When the above code is compiled and executed, it will produce the following results

255
145
230
205
130
143
152
360

Display element in three dimension Array using nested while loop

Program 1

class ThreeDarray_while{
public static void main(String args[]){
int[][][] array;  //array declaration
array=new int[2][2][3]; //array initiation
array[0][0][0]=967; //Assign value to array
array[0][0][1]=157;                
array[0][0][2]=247;          
array[0][1][0]=933;         
array[0][1][1]=625;             
array[0][1][2]=897;             
array[1][0][0]=900;        
array[1][0][1]=287;          
array[1][0][2]=475;             
array[1][1][0]=866;            
array[1][1][1]=850;             
array[1][1][2]=747; 

int i=0;
while(i<=1){
    
    int j=0;
    while(j<=1){
        int k=0;
        while(k<=2){
        System.out.println(array[i][j][k]);
                k++;
                }
            j++;
            }
        i++;
        }
    }
}

 

When the above code is compiled and executed, it will produce the following results

967
157
247
933
625
897
900
287
475
866
850
747

 

 Program 2

class Threedim_ArrayWhile{
public static void main(String args[]){
int[][][] arr={
                {
                {345,687,473},
                {56,97,23},
                {56,87,906}
                               },

                {
                {389,437,323},
                {376,987,453},
                {567,659,42}
                         },
                         };
                         
        int i=0; 
        while(i<=1){
          int j=0;
           while(j<=2){
           int k=0;
              while(k<=2){
           
           System.out.println(arr[i][j][k]);
           k++;
        }
        j++;
        }
        i++;
        }
        
        }     
                         
    }

When the above code is compiled and executed, it will produce the following results

345
687
473
56
97
23
56
87
906
389
437
323
376
987
453
567
659
42

 

 

Suggested for you

for loop in Java

While loop in Java

Nested for loop in Java

Nested while loop in Java

 

Two Dimensional Array in Java language
Three dimension Array in C programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago