Kevin Ruiz :
How would I print coordinates from my 2d array? I have to print the coordinates from wherever the number 5 is. The numbers are randomly generated into the array using math.random. here is the code
public class MultiArrays
{
public static void main(String[] args)
{
int[][] array = new int[3][4];
for( int index = 0; index< array.length; index++)
{
for (int j = 0; j < array[index].length; j++)
{
array[index][j] = (int)(Math.random()*10);
}
}
for(int column = 0; column < array[0].length; column++)
{
for(int row = 0; row < array.length; row++)
{
System.out.print(array[row][column]+" | ");
}
System.out.println();
}
}
}
Mershel :
I think what you are looking for is:
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length; col++) {
if(array[row][col] == 5) {
System.out.println("Row: " + row + " Col: " + col + " has value 5");
}
}
}
It treats first row / col as 0 (starting from 0)