import java.util.Scanner;
public class Maze {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows = 0;
int cols = 0;
String arrayLine = "";
int counter = 0;
rows = sc.nextInt();
cols = sc.nextInt();
arrayLine = sc.next();
char[][] array = new char[rows][cols];
for(int r=0; r<rows; r++){
for (int c=0; c<cols; c++){
array[r][c] = arrayLine.charAt(counter);
counter ++;
}
}
System.out.println(array);
System.out.println();
}
}
我从中引入信息的文档是:
8
7
000000011111S0000000110101111010101100010110111011010E00
我运行它时得到的输出是 [[C@252f0999
请帮忙,我才刚刚开始学习java!
最佳答案
array
是一种特殊的对象,它没有隐式的 toString()
来管理数组内容的 pretty-print ,发生的事情是该对象由对象的标准表示表示,即它的哈希码。
你应该使用 Arrays.toString()
:
for (int i = 0; i < array.length; ++i)
System.out.println(Arrays.toString(array[i]));
请注意,您不能直接编写 Arrays.toString(array)
因为,如文档中所述:
If the array contains other arrays as elements, they are converted to strings by the Object.toString() method inherited from Object, which describes their identities rather than their contents.
关于java - 二维数组,输出不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14544623/