0

如下所示,我的方法 locateLargest() 是一种在数组中查找最大值坐标的方法。我无法将返回值放入 toString 方法。我不知道如何将其格式化为 toString 方法。

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);
    }
}

我的 toString 尝试:

public String toString(){
    return "[" + i + "][" + j + "]";
}

位置类代码:

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;
    }

这是我的程序的完整代码:

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(l1.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

6 回答 6

0

toString 方法具有标题

public String toString()

所以你需要做的就是创建一个带有返回字符串的标头的方法。

正如上面提到的那样,您的问题非常模糊,并且 Location 不是标准库类,所以我不能给你任何具体的东西,但是像

public String toString() {
    return "(" + x + ", " + y + ")";
}

如果您希望 toString 表示一个点,则可能接近您的目标。

于 2013-11-18T00:40:38.563 回答
0

如果 Location 是你自己定义的类,你也应该实现函数 toString()

class Location{
    int locationX,locationY;    

    public Location(int x,int y){
        locationX = x;
        locationY = y;
    }

    public String toString(){
        return locationX+","+locationY;
    }
}
于 2013-11-18T00:41:11.573 回答
0

你需要什么转换成字符串?不管怎样,不管你做什么,把它加到最后:

.toString();

您可以创建一个字符串变量并返回它:

StringVariable = <object>.toString();

或者只是打印它或做任何其他事情

System.out.println(<object>.toString());

希望有帮助。如果它没有在评论中告诉我!

于 2013-11-18T00:42:14.200 回答
0

你应该使用row column

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

您不能使用,i j因为.i jLocation

public String toString(){
    return "[" + i + "][" + j + "]";
}

只要 this (下面)是您的构造函数,而不是您之前使用xLocationand的构造函数,yLocation那么您应该没问题。

Location(int row, int column){
    this.row = row;
    this.column = column;
}//end arg constructor
于 2013-11-18T00:45:02.837 回答
0

我不确定这是否有帮助,但也许下面的代码有帮助

  return new Location(i,j);
 String toStringI = Integer.toString(i); 
    String ToStringj = integer.toString(j);

或使用 String.valueOf(myInteger);

如果您试图在窗口中显示或输出它,您的类“命名:位置”设置为整数试试这个?

System.out.println("",i,"",j);

随便;p

于 2013-11-18T00:45:57.873 回答
0

最干净的方法是放在你的班级:

public String toString(){
        return String.format("[%i][%i]",row,column);
    }
于 2013-11-18T00:48:36.933 回答