java - 二维数组,输出不正确

标签 java arrays 2d output

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/

相关文章:

Java Swing 处理前更新

Python 使用自定义键对 2D 列表进行排序

java - 读取 .txt 文件并存储在二维字符数组 java

java - 查找 Java 字符串中出现的字符

java - 使用一个迭代器删除多个列表的条目

javascript - 如何将字符串转换为 React 组件

java - 球离开屏幕

c++ - 通过复制到函数将指针传递给动态分配的数组会产生意外结果

javascript - 我的数组包含一个空格 [""]。当我用下划线加入空格时,结果字符串中的空格元素在 html 中不可见

c - 从数组插入链表节点,但并非所有节点都被插入