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

Iterate 2D array in Java

Filed under: Java on 2023-08-23 06:49:05

// Program to use Two dimensional Array

class TwoDArray 
{
public static void main(String args[ ]) 
{
 int a[ ][ ]= new int[2][3];
 int i, j, x = 1;
 for(i=0; i<2; i++)
 {
  for(j=0; j<3; j++) 
  {
   a[ i ][ j ] = x;
   x++;
  }
 }
 for(i=0; i<2; i++) 
 {
  for(j=0; j<3; j++)
   System.out.print(a[ i ][ j ] + "   ");
  System.out.println();
 }
}
}


Output:

1  2  3
4  5  6

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