Oliver Burdekin - 9 months ago 31

Java Question

I'm tired. This must be simple. wood... for.... trees....

I'm trying to return the position of a specific value in a 2D array.

I have a double array [300][300].

All values contained in it are 0 apart from one which is 255.

How do I write a method to return the [i][j] location of 255?

Thanks in advance.

Answer

Simply iterate over all the elements until you find the one that is `255`

:

```
for ( int i = 0; i < 300; ++i ) {
for ( int j = 0; j < 300; ++j ) {
if ( array[i][j] == 255 ) {
// Found the correct i,j - print them or return them or whatever
}
}
}
```