Arrays in Java : Please can anu one explain the output

king99

king99

@king99-bh2V1z Oct 26, 2024

Hi techies ,

I am new to java and trying to understand arrays but was stuck in concepts , Please any one can explain the following is the code :

class ThreeDMatrix
{

public static void main(String args[])
{
int threeD[][][] = new int[3][4][5];
int i, j, k;
for(i=0; i<3; i++)
for(j=0; j<4; j++)
for(k=0; k<5; k++)
threeD[j][k] = i * j * k;
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
for(k=0; k<5; k++)
System.out.print(threeD[j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}

Output of this program is :

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

I would be thankful for the help ,Regards King99 !!

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Varsha0802

    Varsha0802

    @varsha0802-YohNRX Oct 12, 2011

    The program's full output is:

    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0

    0 0 0 0 0
    0 1 2 3 4
    0 2 4 6 8
    0 3 6 9 12

    0 0 0 0 0
    0 2 4 6 8
    0 4 8 12 16
    0 6 12 18 24

    The output is self explanatory, As the array indexes start by 0, the first matrix contain all zeros because i's value will be zero for it.

  • king99

    king99

    @king99-bh2V1z Oct 12, 2011

    Hi Varsha Thanks for reply , Please is it possible for you to give line wise explanation , It would be helpful !!

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Oct 12, 2011

    It means 3 blocks , 4 rows and 5 columns.

    For 1 block, it will create 4 rows and for one row it will create 5 columns