
print("code sample");how to display all elements in 2d array usin any one loop

Hi Friend,
Try the following code:
class Display2DWithOneLoop {
public static void main(String args[]) {
int i=0,j=0;
int array[][]={ {1,2,3,4}, {5,6,7,8}};
while(true) {
System.out.print(array[i][j] + " ");
if (array[i].length == j + 1) {
j = 0;
i++;
System.out.println();
} else {
j++;
}
if(array.length == i) {
break;
}
}
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.