1

I have a 2D int[][] and am trying to write a function that locates a 0 in that array and returns an array with its coordinates.

I came up with this:

public int[] locateZero(int[][] array) {
    int[] coordinates = new int[2];
    for (int i=0; i < array.length; i++) {
        for (int j=0; j < array[0].length; j++) {
            if (array[i][j] == 0) {
                //The following line doesn't work!
                coordinates.add(i, j);
            }
        }
    }
    return coordinates;
}

NetBeans keeps underlying the add method, stating that it cannot find it.

Could someone help me, please?

It's a stupid problem, I know. I'm a Java noob.

3
  • Your IDE is flagging it but does it still work when you run or are you getting an error? Commented Apr 21, 2018 at 15:18
  • 1
    coordinates is not a list there are no way to use coordinates.add(i, j); with array Commented Apr 21, 2018 at 15:19
  • I think you want to do this coordtinates[0] = i; and coordtinates[1] = j; Commented Apr 21, 2018 at 15:23

3 Answers 3

4

Your array named coordinates is an array. Arrays do not support an add() function. If you want to have an add function, use ArrayList<Integer> instead.

What would be more typical though, would be to assign the values to your array like so:

public int[] locateZero(int[][] array) {
    int[] coordinates = new int[2];
    for (int i=0; i < array.length; i++) {
        for (int j=0; j < array[0].length; j++) {
            if (array[i][j] == 0) {
                //here is the difference
                coordinates[0] = i;
                coordinates[1] = j;
            }
        }
    }
    return coordinates;
}
Sign up to request clarification or add additional context in comments.

Comments

2

As already said you can't use add. Arrays don't support this. But I won't use an array to return the coordinates. I would write a simple class to store them.

class Coordinate
{
    public int coordX;
    public int coordY;

    Coordinate(int x, int y)
    {
        this.coordX = x;
        this.coordY = y;
    }
}

private Coordinate locateZero(int [][] array)
{
    if(array == null)
    {
        return null;
    }

    for(int x = 0; x < array.length; x++)
    {
        for(int y = 0; y < array[0].length; y++)
        {
            if(array[x][y] == 0)
            {
                return new Coordinate(x, y);
            }
        }
    }

    return null; // Value zero not found in array
}

// Usage of Coordinate class

Coordinate coords = locateZero(myArray);

if(null != coords)
{
    // Value zero found print coordinates
    System.out.println(coords.coordX);
    System.out.println(coords.coordY);
}

Comments

1

You are having many errors in this program. First of all you are using a array not ArrayList. There is no 'add' method in array in java. Second you want to return 2 values i.e. coordinates then you can simply make a 2D array and return it. So your function will look like this.

public int[][] locateZero(int[][] array) {
    int[][] coordinates = new int[1][1];
    for (int i=0; i < array.length; i++) {
        for (int j=0; j < array[0].length; j++) {
            if (array[i][j] == 0) {
                coordinates[0][0]=i;
                coordinates[0][1]=j;
                break;
            }
        }
    }
    return coordinates;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.