Java int[][] array - iterating and finding value

2019-02-07 02:16发布

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!

7条回答
forever°为你锁心
2楼-- · 2019-02-07 02:58

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-07 03:03

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

查看更多
欢心
4楼-- · 2019-02-07 03:03

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.

查看更多
放荡不羁爱自由
5楼-- · 2019-02-07 03:06

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
    }
}
查看更多
够拽才男人
6楼-- · 2019-02-07 03:13

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.

查看更多
Rolldiameter
7楼-- · 2019-02-07 03:16

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.

查看更多
登录 后发表回答