我有这个代码:
public static int[][] doubleMat(int[][] mat)
{
int row = mat.length;
int col = mat[0].length;
int[][] num = new int[row][col];
for (int x = 0; x < row; x++)
{
for (int y = 0; y < col; y++)
{
num[x][y] = mat[x][y] * 2;
}
}
return num;
这在跑步者文件中:
int[][] mat = {{45,101,87,12,41,0},{12,8,12,8,15,841},{-12,-1,-741,-1,0,74}};
System.out.println(Array2DHelper2.doubleMat(mat));
它不断返回 [[I@4617c264 而不是二维数组。我很确定它与 toString 有关,但我不知道该怎么做。我还必须将其作为整个阵列打印,而不是打印每个单独的点。
我尝试使用 Arrays.toString(Array2DHelper2.doublemat(mat)) 打印它,我认为这会起作用,但它打印更多类似于 [[I@4617c264 (我忘了它们叫什么)的文本。
使用
Arrays.deepToString()
表示多维数组:
import java.util.Arrays;
class Main {
public static int[][] doubleMat(int[][] mat) {
int row = mat.length;
int col = mat[0].length;
int[][] num = new int[row][col];
for (int x = 0; x < row; x++) {
for (int y = 0; y < col; y++) {
num[x][y] = mat[x][y] * 2;
}
}
return num;
}
public static void main(String[] args) {
int[][] mat = { { 45, 101, 87, 12, 41, 0 }, { 12, 8, 12, 8, 15, 841 }, { -12, -1, -741, -1, 0, 74 } };
System.out.printf("Before: %s%n", Arrays.deepToString(mat));
System.out.printf("After: %s%n", Arrays.deepToString(doubleMat(mat)));
}
}
输出:
Before: [[45, 101, 87, 12, 41, 0], [12, 8, 12, 8, 15, 841], [-12, -1, -741, -1, 0, 74]]
After: [[90, 202, 174, 24, 82, 0], [24, 16, 24, 16, 30, 1682], [-24, -2, -1482, -2, 0, 148]]