You are here: Home / Topics / Initialise 2D array in Java

Initialise 2D array in Java

Filed under: Java on 2023-08-23 06:54:46

// Program to initialize 2D array with expression

class  InitTwoDWithExp 
{
public static void main(String args[ ]) 
{
 double a[ ][ ] = {
   { 0*0, 1*0, 2*0, 3*0 },
   { 0*1, 1*1, 2*1, 3*1 },
   { 0*2, 1*2, 2*2, 3*2 },
   { 0*3, 1*3, 2*3, 3*3 } };
 int  i, j;

 for( i=0 ; i<4 ; i++ ) 
 {
  for( j=0 ; j<4 ; j++ )
  {
   System.out.print( a[ i ][ j ] + "    " );
  }
  System.out.println();
 }
}
}


Output:

0.0   0.0   0.0   0.0
0.0   1.0   2.0   3.0
0.0   2.0   4.0   6.0
0.0   3.0   6.0   9.0

 


About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.