简体   繁体   中英

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

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.

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.

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.

You can define your own type 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);
}

Use something like this, which would return a Point object:

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.

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

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

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM