Iterating over a 2 D array

To iterate over a 2 d array you need to iterate over rows and take one row at a time inside loop and iterate over each column of it .

Here is sample program of iterating over a 2 D array

package test;

public class TwoDArray {



public static void main(String[] args) {

int i=01;
System.out.println(i);
int[][] chessBoard=new int[8][8];

//First we will insert Values  in 2 d array

for(int row=0; row<chessBoard.length;row++){
for(int column=0; column<chessBoard[row].length; column++){
String combine=String.valueOf(row)+String.valueOf(column);
//System.out.println(combine);
chessBoard[row][column]=Integer.parseInt(combine);
}
}

//Here we are getting values from a 2D array.

for(int row=0; row<chessBoard.length; row++){
for(int col=0; col<chessBoard[row].length;col++){

System.out.println(chessBoard[row][col]);


}



}


}

}

Comments

Popular posts from this blog

Integration Control

Zensar Java interview questions