Java : 가치를 tostring 메서드에 넣는 데 어려움을 겪습니다
문제
My Method LocateLastGest () 아래에 표시된 표시는 내 배열에서 가장 큰 값의 좌표를 찾는 방법입니다.복귀 값을 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 + "]";
}
}
. 해결책
위치가 클래스 인 자신의 클래스 인 경우, tostring () 함수를 구현해야합니다
class Location{
int locationX,locationY;
public Location(int x,int y){
locationX = x;
locationY = y;
}
public String toString(){
return locationX+","+locationY;
}
}
. 다른 팁
tostring 메소드는 헤더가있다
public String toString()
.
그렇게해야 할 모든 것이 문자열을 반환하는 헤더를 사용하는 방법을 작성하는 것입니다.
귀하의 질문은 위에서 언급 한 사람만큼 꽤 모호하고 위치는 표준 라이브러리 수업이 아니므로 구체적인 것을 줄 수는 없지만
와 같은 것입니다.public String toString() {
return "(" + x + ", " + y + ")";
}
.
tostring이 점을 나타내는 것을 원한다면 당신이가는 것에 가깝습니다.
문자열로 변환해야합니까? 어쨌든, 당신이하는 일은 무엇이든, 이것을 끝까지 추가하십시오 :
.toString();
.
문자열 변수를 만들고 반환 할 수 있습니다.
StringVariable = <object>.toString();
.
또는 그냥 인쇄하거나 다른 것을 수행하십시오
System.out.println(<object>.toString());
.
도움이되었습니다.그것이 그것에 대해 그것에 대해 말하지 않았다면!
가장 깨끗한 방법은 수업에 넣는 것입니다 :
public String toString(){
return String.format("[%i][%i]",row,column);
}
. IM이 도움이되는지 모르겠지만, 어쩌면 아래 코드가 도움이 될 수 있습니다
return new Location(i,j);
String toStringI = Integer.toString(i);
String ToStringj = integer.toString(j);
.
또는 사용하여 String.ValueOf (MyInteger);
클래스 "named : location"은 창에 표시하거나 출력하려고 시도하는 경우 정수로 설정되어 있습니까?
System.out.println("",i,"",j);
.
뭐든지; p
COULLRD는 row
column
public String toString(){
return "[" + row + "][" + column + "]";
}
.
i
j
가 존재하지 않기 때문에 i
j
Location
를 사용할 수 없습니다.
public String toString(){
return "[" + i + "][" + j + "]";
}
.
이 (아래)가 생성자이며 xLocation
및 yLocation
로 이전에 있었던 것과 마찬가지로
Location(int row, int column){
this.row = row;
this.column = column;
}//end arg constructor
.