JAVA:Problème pour mettre des valeurs dans une méthode toString
Question
Ma méthode LocateLargest() comme indiqué ci-dessous est une méthode pour trouver les coordonnées de ma plus grande valeur dans mon tableau.J'ai du mal à mettre les valeurs de retour dans une méthode toString.Je ne sais pas comment le formater dans la méthode 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);
}
}
Ma tentative toString :
public String toString(){
return "[" + i + "][" + j + "]";
}
code de classe d'emplacement :
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;
}
Voici mon code complet pour mon programme :
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 + "]";
}
}
La solution
Si l'emplacement est une classe, vous avez défini vous-même, vous devez implémenter la fonction ToString () également
class Location{
int locationX,locationY;
public Location(int x,int y){
locationX = x;
locationY = y;
}
public String toString(){
return locationX+","+locationY;
}
}
Autres conseils
Une méthode de tostrage a l'en-tête
public String toString()
Donc, tout ce que vous avez à faire est de faire une méthode avec cet en-tête qui renvoie une chaîne.
Votre question est assez vague comme une personne notée ci-dessus, et l'emplacement n'est pas une classe de bibliothèque standard, donc je ne peux donc rien vous donner spécifique, mais quelque chose comme
public String toString() {
return "(" + x + ", " + y + ")";
}
Peut-être être proche de ce que vous allez pour si vous voulez une touche de représentation pour représenter un point.
Que devez-vous convertir en chaîne? Quoi qu'il en soit, tout ce que vous faites, ajoutez ceci à la fin:
.toString();
Vous pouvez faire une variable de chaîne et le renvoyer:
StringVariable = <object>.toString();
ou simplement l'imprimer ou faire autre chose
System.out.println(<object>.toString());
espère que cela a aidé.Si cela ne m'a pas parlé de cela dans les commentaires!
La manière la plus propre serait de mettre dans votre classe:
public String toString(){
return String.format("[%i][%i]",row,column);
}
Je ne suis pas sûr que cela aide, mais peut-être peut-être que les codes ci-dessous aident
return new Location(i,j);
String toStringI = Integer.toString(i);
String ToStringj = integer.toString(j);
ou en utilisant String.valueof (myinteger);
Votre classe "nommée: emplacement" est définie sur Entier si vous essayez de le montrer dans la fenêtre ou de la sortie, essayez-la?
System.out.println("",i,"",j);
peu importe; p
Vous devriez utiliser row
column
public String toString(){
return "[" + row + "][" + column + "]";
}
Vous ne pouvez pas utiliser i
j
parce que non i
j
existe dans Location
.
public String toString(){
return "[" + i + "][" + j + "]";
}
Tant que ceci (ci-dessous) est votre constructeur et non ce que vous aviez auparavant avec xLocation
et yLocation
, ça devrait aller.
Location(int row, int column){
this.row = row;
this.column = column;
}//end arg constructor