codescracker


java

Java Three Dimensional Array Program



« Previous Program Next Program »


Three Dimensional Array Program

Three dimensional array is a complicated concept in programming that contains three loops. So to print or initialize a three dimensional array, you have to use the three loops. The innermost loop makes one dimensional array and the second innermost loop contain the two dimensional array whereas the outer loop contains the three dimensional array.

Java Programming Code for Three Dimensional (3D) Array

A three dimensional (3D) array can be thought of as an array of arrays of arrays. Following is a simple Java Program to initialize three dimensional (3D) array of 3*4*2 automatically i.e., it initialize the natural numbers starting from 1, then the following program prints all the number along with their indexing on the screen :

/* Java Program Example - Three Dimensional Array Program */
		
import java.util.Scanner;

public class JavaProgram
{
   public static void main(String args[])
   {
      int arr[][][] = new int[3][4][2];
      int i, j, k, num=1;
	  
      for(i=0; i<3; i++)
      {
          for(j=0; j<4; j++)
          {
              for(k=0; k<2; k++)
              {
                  arr[i][j][k] = num;
                  num++;
              }
          }
      }
	  
       for(i=0; i<3; i++)
      {
          for(j=0; j<4; j++)
          {
              for(k=0; k<2; k++)
              {
                  System.out.print("arr[" +i+ "][" +j+ "][" +k+ "] = " +arr[i][j][k]+ "\t");
              }
              System.out.println();
          }
          System.out.println();
      }
   }
}

When the above Java Program is compile and executed, it will produce the following output:

three dimensional array program in java

Here, outer array has three elements, each of which is a 2D array of four 1D arrays, each of which contains two integers. It means, a 1D array of the two elements is constructed first. Then four such 1D arrays are placed one below the other to give a 2D array containing four rows. Then, three such 2D arrays are placed one behind the other to yield a 3D array containing three 2D arrays.

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test