Java 返回数组的问题

Java 返回数组的问题,java,arrays,Java,Arrays,我有一个2D int[]],我正试图编写一个函数,在该数组中定位一个0并返回一个带坐标的数组 我想到了这个: 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 (arr

我有一个2D int[]],我正试图编写一个函数,在该数组中定位一个0并返回一个带坐标的数组

我想到了这个:

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;
}
public int[]locateZero(int[]]数组){
int[]坐标=新的int[2];
for(int i=0;i
NetBeans保持
add
方法的底层,声明找不到它

有人能帮帮我吗


我知道这是个愚蠢的问题。我是一个Java noob。

您名为坐标的数组是一个数组。数组不支持
add()
函数。如果要使用add函数,请改用
ArrayList

但更典型的是,将值分配给数组,如下所示:

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;
}
public int[]locateZero(int[]]数组){
int[]坐标=新的int[2];
for(int i=0;i
名为
坐标的数组是一个数组。数组不支持
add()
函数。如果要使用add函数,请改用
ArrayList

但更典型的是,将值分配给数组,如下所示:

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;
}
public int[]locateZero(int[]]数组){
int[]坐标=新的int[2];
for(int i=0;i
如前所述,您不能使用add。阵列不支持此操作。但我不会使用数组返回坐标。我将编写一个简单的类来存储它们

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);
}
类坐标
{
公共国际合作社;
公共国际合作;
坐标(整数x,整数y)
{
this.coordX=x;
this.coordY=y;
}
}
专用坐标定位零(int[][]数组)
{
if(数组==null)
{
返回null;
}
对于(int x=0;x
如前所述,您不能使用add。阵列不支持此操作。但我不会使用数组返回坐标。我将编写一个简单的类来存储它们

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);
}
类坐标
{
公共国际合作社;
公共国际合作;
坐标(整数x,整数y)
{
this.coordX=x;
this.coordY=y;
}
}
专用坐标定位零(int[][]数组)
{
if(数组==null)
{
返回null;
}
对于(int x=0;x
此程序中有许多错误。 首先,您使用的是数组而不是ArrayList。java中的数组中没有“add”方法。 其次,您希望返回2个值,即坐标,然后您可以简单地创建一个二维数组并返回它。因此,您的函数将如下所示

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;
}
public int[]]locateZero(int[]]array){
int[][]坐标=新的int[1][1];
for(int i=0;i
此程序中有许多错误。 首先,您使用的是数组而不是ArrayList。java中的数组中没有“add”方法。 其次,您希望返回2个值,即坐标,然后您可以简单地创建一个二维数组并返回它。因此,您的函数将如下所示

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;
}
public int[]]locateZero(int[]]array){
int[][]坐标=新的int[1][1];
for(int i=0;i
您的IDE正在标记它,但它在您运行时是否仍然有效,或者您是否遇到错误?
坐标
不是一个列表,无法使用
坐标。添加(i,j)使用ArraI我想你应该这样做
协调[0]=i
协调[1]=j
;您的IDE正在标记它,但它在您运行时是否仍然有效,或者您是否收到错误?
坐标
不是一个列表,无法使用
坐标。添加(i,j)使用ArraI我想你应该这样做
协调[0]=i
协调[1]=j