Two Dimensional Array Program

Ads
 

Two Dimensional Array Program

This page discusses - Two Dimensional Array Program

This page discusses - Two Dimensional Array Program

Two Dimensional Array Program Using Nested for loop

     

This is a simple Java array  program . In this session we will teach how to use of a two dimensional array. First of all we will define a class "Matrix". In this program we are going to make two dimensional array having three row and three columns. 

By using the multidimensional array we are going to make a matrix. For that we have to use a nested for loop. The for loop takes three parameters one is the initialization value, second, the place up to where the loop runs and the third one is the counter. Now declare one boolean type of variable found which will indicate whether the array has reached its limit or not.  Please go through this program properly so that you can understand what is happening in this program.

Here is the code of this Program

import java.io.*;
class Matrix{
 public static void main(String[] args){
  int[][] matrix = new int[3][3];
  for (int i =0; i < 3; i++){
 for (int j =0; j < 3; j++){
  matrix[i][j(i+1(j+1);
 }
  }
  boolean found = false;
  int i = 0;
  while (!found && i < 3){
 int j = 0;
 while (!found && j < 3){
  if (matrix[i][j==  2){
  found = true;
  }
 }
  }
  System.out.println("The matrix is:");
  for (i=0; i < 3; i++){
 for (int j=0; j <3; j++){
  System.out.print(matrix[i][j" ");
 }
 System.out.println();
  }
  System.out.println();
 }
}


Download this Example

Ads

Ads