# Search a two Dimensional Array

• 09-13-2011, 03:25 PM
djbeko06
Search a two Dimensional Array
How do u sort and search a two dimensional array for a key e.g 4 and return the index of the key maybe 3????

- -
| 1 8 0 |
| 4 8 4 |
| 2 6 9 |
- -

Thanks:D:
• 09-13-2011, 03:44 PM
KevinWorkman
Re: Search a two Dimensional Array
That's not how this works. What have you tried? Please read the link in my signature on asking questions the smart way.

How would you do this by hand, without a computer?
• 09-13-2011, 05:06 PM
sehudson
Re: Search a two Dimensional Array
Sorts and searches are 2 different things. If you want to search the 2d array for a value (assuming it is 'unsorted'), you need to step through each element in the 2d array and determine if it equals the value you are looking for. Let's say this is the array that you are searching:

Code:

```int[][] myArray = new int[y][x]; //Populate the array with data //Now ready to search```

Use the size declarations(y,x) that you made when you allocated the array to help determine your for loop bounds.

Code:

```for(int row=0; row<y; row++){   for(int col=0; col<x; col++{   } }```