JAVA:无法将值放入toString方法

时间:2013-11-18 00:34:25

标签: java arrays locate

我的方法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 + "]";
    }
}

6 个答案:

答案 0 :(得分:0)

toString方法有标题

public String toString()

所以你需要做的就是用一个返回String的标头创建一个方法。

如上所述,您的问题非常模糊,而且位置不是标准的图书馆类,所以我不能给您任何具体的内容,但是

public String toString() {
    return "(" + x + ", " + y + ")";
}
如果你想让一个toString代表一个点,那么

可能接近你的目的。

答案 1 :(得分:0)

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

class Location{
    int locationX,locationY;    

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

    public String toString(){
        return locationX+","+locationY;
    }
}

答案 2 :(得分:0)

您需要将什么转换为String? 无论如何,无论你做什么,都要把它添加到最后:

.toString();

您可以创建一个String变量并将其返回:

StringVariable = <object>.toString();

或者只是打印或做其他事情

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

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

答案 3 :(得分:0)

你应该使用row column

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

您无法使用i j,因为i中不存在j Location

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

只要这个(下面)是您的构造函数而不是之前使用xLocationyLocation的内容,您应该没问题。

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

答案 4 :(得分: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

答案 5 :(得分:0)

最干净的方式是上课:

public String toString(){
        return String.format("[%i][%i]",row,column);
    }
相关问题