vote up 1 vote down
star

Hi

I have an array in the form of 'int[][]' that represents the co-ordinates of a small grid. Each co-ordinate has been assigned its own value. eg array[0][4] = 28......

I have two questions. Firstly, how do I iterate through all the stored values. Secondly, I want to be able to input a value and have its specific co-ordinates in the grid returned. What would be the best way to approach this?

Thank you for any help!

flag
If this is homework, you should be clear about this (in addition to using the homework tag). – James Van Huis Jan 23 at 22:12
add comment

8 Answers:

vote up 6 vote down
check

You can iterate with either for loops or enhanced for loops:

for (int row=0; row < grid.length; row++)
{
    for (int col=0; col < grid[row].length; col++)
    {
        int value = grid[row][col];
        // Do stuff
    }
}

or

// Note the different use of "row" as a variable name! This
// is the *whole* row, not the row *number*.
for (int[] row : grid)
{
    for (int value : row)
    {
         // Do stuff
    }
}

The first version would be the easiest solution to the "find the co-ordinates" question - just check whether the value in the inner loop is correct.

link|flag
+1 for completeness but I would rename 'i' and 'j' to 'row' and 'col'. – Outlaw Programmer Jan 23 at 20:55
Oh, okay then :) Now watch me fail to do it consistently... – Jon Skeet Jan 23 at 20:56
But Outlaw, if he does that, he won't be paying homage to Fortran. – Paul Tomblin Jan 23 at 20:57
@Paul: Nah. Fortran was paying homage to this answer's first version. It may not have known it, but it's surely true. – Jon Skeet Jan 23 at 21:00
@Outlaw ... except which dimension is the rows and which dimension is the columns is totally dependent on your interpretation (map[row][column] or map[column][row]); however, i, j, k, l, m... are always silly_array[i][j][k][l][m].... Which I suppose is what Jon was getting at. – zacherates Jan 23 at 21:02
add / show 1 more comment
vote up 2 vote down

to iterate over the values use loops:

 int[][] matrix   
 //...
 for(int row[] : matrix)
     for(int cell : row){
      //do something with cell
    }

to access the coordinates based on the value you would need some sort of double hashmap (look a at java.util.HashMap) but i am aware of nothing that does so directly

link|flag
You could do it with a regular hashmap. You'd just need to define an object to use as the Key that has both coordinates in it. – Herms Jan 23 at 20:53
@Herms :He wanats to find the coordinates by the value. so the key must be the cell value . things will get more complicated if multiple cells can have the same value, but it is still doable – nickolai Jan 23 at 20:56
I think having another datastructure here is probably a bad idea. It might be tough to keep them in sync. It's probably better to search the whole table, even though it will be slower. – Outlaw Programmer Jan 23 at 21:02
Store new Integer(ix + iy * 1000) as the value in your hash table. If your y index can go over 1000 use a bigger number--ints are really big. To get it back use ix=val%1000, iy=val/1000. – Bill K Jan 23 at 21:22
add comment
vote up 0 vote down

Unless your grid is sorted in some way then you probably won't do any better than a brute force search.

For iterating, I think it would be something like this (syntax might be off a bit, I haven't dealt with arrays in java for a while.):

int[][] grid;  // just assuming this is already assigned somewhere

for(int x = 0 ; x < grid.length ; x++) {
  int[] row = grid[x];
  for(int y = 0 ; y < row.length ; y++) {
    int value = row[y];
    // Here you have the value for grid[x][y] and can do what you need to with it
  }
}

For searching you'd probably need to use that to iterate, then return once you've found it.

If you might be looking up the position of the same value multiple times then you might want to memoize the results using a hashtable.

link|flag
add comment
vote up 0 vote down

To iterate over all the elements in the grid try this:

int grid[][] = new int[10][10];

for(int i = 0; i < grid.length(); ++i) {
    for(int j = 0; j < grid[i].length(); ++j) {
        // Do whatever with grid[i][j] here
    }
}
link|flag
add comment
vote up 0 vote down

Use nested for loops to iterate over the x and y dimensions, which lets you go over each value, one at a time.

For inputting a value, just do the same as above, but look for a match to your requested value.

link|flag
add comment
vote up 0 vote down

Thanks guys, your advice has been a great help!

link|flag
add comment
vote up 0 vote down

You'll be happiest if you block all these collections inside a single class and don't expose them in any way.

This means moving your search and lookup routines into this class as well.

For storage, everybody's covered iterating, add a hashtable and a lookup. I put this comment on nickolai's post:

Store new Integer(ix + iy * 1000) as the value in your hash table. If your y index can go over 1000 use a bigger number--ints are really big. To get it back use ix=val%1000, iy=val/1000.

If your array and hashtable are encapsulated in the same class, the rest of your code will be pretty easy to write and a lot cleaner.

link|flag
add comment
vote up -1 vote down

There's generally no way to find the specific coordinates of a particular value except by going through the array and searching for it. However, if the values in the array are guaranteed to be unique (i.e. each value only occurs in one cell), you could maintain a separate array as an index, which stores the coordinates of each value indexed by the value.

link|flag
Misleading, a hashtable does it easily. – Bill K Jan 23 at 21:24
add comment

Your Answer:

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.