Java:如何从二维双精度数组中获取指定坐标的整数 (x,y)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11296542/
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-10-31 04:33:30  来源:igfitidea点击:

Java: How do I get the integer (x,y) of a specify coordinate from a 2d double array

java

提问by Twyla

I have an array of 2d double data say [100][100]. Most of it is fill with '0.0' while there is a block of '1.0' located somewhere inside. I make a loop and are able to locate the '1.0' but have no idea how to extract the x and y (not the value which is '1.0') from it.

我有一个二维双数据数组,比如 [100][100]。其中大部分填充为“0.0”,而内部某处有一个“1.0”块。我做了一个循环,能够找到“1.0”,但不知道如何从中提取 x 和 y(不是“1.0”的值)。

I spent hours searching for a solution. Even tried Arrays.binarySearch method but keep giving me error. Below is my code to loop through the array.

我花了几个小时寻找解决方案。甚至尝试过 Arrays.binarySearch 方法,但一直给我错误。下面是我遍历数组的代码。

int findX() {
  for (int i = 0; i < data.length; i++) {
    for (int j = 0; j < data[i].length; j++) {
      if (data[i][j] == 1.0) {
        int x = i;
      }
      break; // stop search once I found the first '1.0'
             // as there are a couple of them
    }
  }
  return x;

Please help, any advice are greatly appreciated.

请帮助,非常感谢任何建议。

采纳答案by Tudor

You can define your own type Pair:

您可以定义自己的类型Pair

public class Pair {
    private int x;
    private int y;

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}

You can also make it generic if you want to use it for other types.

如果您想将其用于其他类型,也可以将其设为通用。

And then return an object of this type from your search method:

然后从您的搜索方法返回此类型的对象:

public Pair search(double[][] data) {
    int x = -1;
    int y = -1;
    for (int i = 0; i < data.length; i++) {
        for (int j = 0; j < data[i].length; j++) {
            if (data[i][j] == 1.0) {
                x = i;
                y = j;              
                break;
            }

        }
    }
    return new Pair(x, y);
}

回答by nickb

Use something like this, which would return a Pointobject:

使用类似这样的东西,它会返回一个Point对象:

public Point getPoint( double[][] data) {
    for (int i = 0; i < data.length; i++) {
        for (int j = 0; j < data[i].length; j++) {
            if (data[i][j] == 1.0) {
                return new Point(i, j); // Once we found the value, return
            }
        }
    }
    return null;
}

This loops through the data (just as you were), except it stops once it finds the first 1.0. When that value is found, it stops. Then, an object that represents the coordinates is returned, otherwise null is returned. You could return an integer array if you prefer.

这会遍历数据(就像您以前一样),但一旦找到第一个1.0. 当找到该值时,它会停止。然后返回一个表示坐标的对象,否则返回null。如果您愿意,您可以返回一个整数数组。

Now, when you call it, you check if it returned null, if it did, datadidn't have 1.0anywhere. Otherwise, get the X and Y coordinates from the object.

现在,当你调用它,你检查它是否回来null,如果没有,data没有1.0任何地方。否则,从对象获取 X 和 Y 坐标。

Point p = obj.getPoint( data);
if( p != null)
    System.out.println( p.getX() . ', ' . p.getY());

回答by Frank Visaggio

so when you think about this one loop the outer or the inner loop is the x coordinate, and the other loop is the Y coordinate.

所以当你考虑这个循环时,外循环或内循环是 x 坐标,另一个循环是 Y 坐标。

00100 00100 00100

00100 00100 00100

 int yCord;
 int xCord;
 for int y=0;y<3;y++
 {// this loop goes up and down so its the y
       for (int x=0;x<5;x++)
       {// this loop goes left and right so its the x value
            yCord=y;
            xCord=x;
       }
  }

side note below is how to convert a double to an int.

下面的旁注是如何将双精度转换为整数。

    double myDouble = 420.5;
    //Type cast double to int
    int i = (int)myDouble;


    // to go from int to double below
    int j=5;
    double output;
    output=(double)j;

so your position is in the yCord and xCord, make sense?

所以你的位置在yCord和xCord,有意义吗?