无法调用toString方法

时间:2013-11-18 01:02:05

标签: java

我的方法locateLargest()如下所示是一个查找数组中最大值坐标的方法。当我尝试在我的main方法中使用locateLargest方法调用toString方法时,我得到一个错误:无法从类型LocationTest对非静态方法locateLargest(int [] [])进行静态引用。不知道我怎么能解决这个问题。

import java.util.Scanner;

public class LocationTest {

public static void main(String[] args) {
    Scanner numberInput = new Scanner(System.in);
    System.out.println("Enter the number of rows and columns of the array: ");
    int row = numberInput.nextInt();
    int column = numberInput.nextInt();
    Location l1 = new Location(row, column);
    Location l2 = new Location();
    row = l1.getRow();
    column = l1.getColumn();
    int[][] array = new int[l1.getRow()][l1.getColumn()];
    System.out.println("Please enter the array elements: ");
    for (int r = 0; r < array.length; r++){
        for (int c = 0; c < array[r].length; c++){
            array[r][c] = numberInput.nextInt();
        }//end nested loop

    }//end for loop
    System.out.println(getMax(array));
    System.out.println(locateLargest(array).toString());

}



    public static int getMax(int[][] x){
        int max = Integer.MIN_VALUE;;
        for (int i = 0; i < x.length; i++){
            for (int j = 0; j < x[i].length; j++){

                if (x[i][j] > max)

                    max = x[i][j];
            }
    }
        return max;
    }


    public Location locateLargest(int[][] x){ 
        int maxValue = getMax(x);
        for (int i = 0; i < x.length; i++){
            for (int j = 0; j < x.length; j++)
                if (x[i][j] == maxValue)
                    return new Location(i,j);
        }
        return null;
    }
}
class Location {
private int row;
private int column;

Location(){}//end constructor

Location(int row, int column){
    this.row = row;
    this.column = column;
}//end arg constructor

public int getRow(){
    return this.row;
}

public int getColumn(){
    return this.column;
}

public String toString(){
    return "[" + row + "][" + column + "]";
}

}

4 个答案:

答案 0 :(得分:1)

函数locateLargest()是非静态函数,因此您无法从static函数(例如main())调用它。将locateLargest()更改为static,因为它似乎没有使用任何实例变量。

您看到编译错误的原因是static函数不允许调用非static函数。

答案 1 :(得分:0)

locateLargest(int[][] x)设为静态方法。问题是非静态方法locateLargest(int[][] x)无法从静态方法main()

访问
public static Location locateLargest(int[][] x){ 

}

答案 2 :(得分:0)

更改

public Location locateLargest(int[][] x){ 

public static Location locateLargest(int[][] x){

BTW,在这种情况下不需要调用toString()。你可以简单地写一下:

System.out.println(locateLargest(array));

答案 3 :(得分:0)

此方法应该是静态的

public static Location locateLargest(int[][] x){ 
    int maxValue = getMax(x);
    for (int i = 0; i < x.length; i++){
        for (int j = 0; j < x.length; j++)
            if (x[i][j] == maxValue)
                return new Location(i,j);
    }
    return null;
}

static方法(在您的情况下为main)无法调用方法或其方法之外的任何变量static。虽然,fyi,非static方法可能会调用static变量方法。

此外,就像您在getmax方法中所做的那样,您可能需要像这样的内部循环

for (int j = 0; j < x[i].length; j++)

instead of

for (int j = 0; j < x.length; j++)

下面

public static Location locateLargest(int[][] x){ 
    int maxValue = getMax(x);
    for (int i = 0; i < x.length; i++){
        for (int j = 0; j < x[i].length; j++)
            if (x[i][j] == maxValue)
                return new Location(i,j);
    }
    return null;
}