在java中搜索二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4376915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:41:30  来源:igfitidea点击:

search a 2 dimensional array in java

javaarrays

提问by system

How does one iterate through a two dimensional array search for [ ] [Name]?

如何遍历二维数组搜索 [] [Name]?

When the Name is found the Index should be returned so that I can change the values in that array.

找到名称后,应返回索引,以便我可以更改该数组中的值。

[Index] [Values].

[索引] [值]。

Also, how does the syntax look for the storing to the array found? [ ] [index]. Loop through index and set a value. [0] [1] = blah.

此外,语法如何查找存储到找到的数组?[ ] [指数]。循环遍历索引并设置一个值。[0] [1] = 废话。

Thanks

谢谢

采纳答案by Andreas Dolk

Sometimes it's easier and always cleaner to put the search in a separate method:

有时将搜索放在单独的方法中更容易也更清晰:

 private Point find2DIndex(Object[][] array, Object search) {

    if (search == null || array == null) return null;

    for (int rowIndex = 0; rowIndex < array.length; rowIndex++ ) {
       Object[] row = array[rowIndex];
       if (row != null) {
          for (int columnIndex = 0; columnIndex < row.length; columnIndex++) {
             if (search.equals(row[columnIndex])) {
                 return new Point(rowIndex, columnIndex);
             }
          }
       }
    }
    return null; // value not found in array
 }

This will return the first match only. If you need all, collect all points in a List and return that list at the end.

这将仅返回第一场比赛。如果您需要全部,请收集列表中的所有点并在最后返回该列表。



Usage:

用法:

private void doSomething() {
  String[][] array = {{"one", "1"},{"two","2"}, {"three","3"}};
  Point index = find2DIndex(array, "two");

  // change one value at index
  if (index != null)
     array[index.x][index.y] = "TWO";

  // change everything in the whole row
  if (index != null) {
     String[] row = array[index.x];
     // change the values in that row
  }

}

回答by morja

Updated due to your comment:

由于您的评论而更新:

for(String[] subarray : array){
   int foundIndex = -1;
   for(int i = 0; i < subarray.length; i++){
      if(subarray[i].equals(searchString)){
         foundIndex = i;
         break;
      }
   } 
   if(foundIndex != -1){
      // change all values that are not at position foundIndex
      for(int i = 0; i < subarray.length; i++){
         if(i != foundIndex){
            subarray[i] = "something";
         }
      } 
      break;
   }
}

回答by Jigar Joshi

the most basic way is to

最基本的方法是

for(int xIndex = 0 ; xIndex < 3 ; xIndex++){
for(int yIndex = 0 ; yIndex < 3 ; yIndex++){
      if(arr[xIndex][yIndex].equals(stringToSearch)){
             System.out.println("Found at"+ xIndex +"," + yIndex);

             for(int remainingIndex = 0 ; remainingIndex  < 3 ; remainingIndex++  ){
                    arr[xIndex][remainingIndex]="NEW VALUES";
             }
             break;
      }
}
}