Home Java Java-conversion Two Dimensional Array Program

Related Tutorials


 
 

Share on Google+Share on Google+

Two Dimensional Array Program

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2011

Related Tutorials

Discuss: Two Dimensional Array Program  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
sayni
January 11, 2012
2D array

how can be understand the program in 2D array per looping and per coding
DMCA.com