JAVA:Probleme beim Einfügen von Werten in eine toString-Methode
Frage
Meine unten gezeigte Methode „locateLargest()“ ist eine Methode, um die Koordinaten meines größten Werts in meinem Array zu finden.Ich habe Probleme beim Einfügen der Rückgabewerte in eine toString-Methode.Ich habe keine Ahnung, wie ich es in die toString-Methode formatieren soll.
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);
}
}
Mein toString-Versuch:
public String toString(){
return "[" + i + "][" + j + "]";
}
Standortklassencode:
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;
}
Hier ist mein vollständiger Code für mein Programm:
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 + "]";
}
}
Lösung
Wenn der Standort eine Klasse ist, die Sie definiert ist, sollten Sie die Funktion TOSTRING () auch als gut implementieren
generasacodicetagpre.Andere Tipps
Eine TOSTRING-Methode hat den Header
generasacodicetagpre.So müssen Sie nur ein Verfahren mit diesem Header erstellen, das eine Zeichenfolge zurückgibt.
Ihre Frage ist ziemlich unbestimmt wie jemand, der oben angegeben ist, und der Standort ist keine Standard-Bibliotheksklasse, also kann ich Ihnen nichts Besonderes geben, aber so etwas wie
generasacodicetagpre.könnte nahe an dem sein, worauf Sie gehen, wenn Sie möchten, dass ein ToString einen Punkt darstellt.
Was müssen Sie in Zeichenfolge konvertieren? Wie auch immer, was auch immer Sie tun, fügen Sie das Ende hinzu:
generasacodicetagpre.Sie können eine String-Variable erstellen und es zurückgeben:
generasacodicetagpre.oder drucken Sie es einfach aus oder tun Sie etwas anderes
generasacodicetagpre.hoffe das geholfen hat.Wenn es mir nicht in den Kommentaren erzählt hat!
Der sauberste Weg wäre, in Ihre Klasse einzutreten:
generasacodicetagpre.Ich bin nicht sicher, ob das hilft, aber vielleicht hilft die folgenden Codes möglicherweise
generasacodicetagpre.oder mithilfe String.Valueof (MyINTEGER);
Ihre Klasse "named: location" ist auf integer gesetzt, wenn Sie versuchen, es im Fenster oder in der Ausgabe zu zeigen, oder dies ausgibt.
generasacodicetagpre.was auch immer; p
Sie sollten verwenden row
column
public String toString(){
return "[" + row + "][" + column + "]";
}
Du kannst es nicht verwenden i
j
weil Nein i
j
existiert in Location
.
public String toString(){
return "[" + i + "][" + j + "]";
}
Solange dies (unten) Ihr Konstruktor ist und nicht das, was Sie vorher hatten xLocation
Und yLocation
, es sollte dir gut gehen.
Location(int row, int column){
this.row = row;
this.column = column;
}//end arg constructor