二维数组,输出几乎不正确 [英] 2D Array, output is no where near correct

查看:32
本文介绍了二维数组,输出几乎不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
    }
}

<小时>

我从中引入信息的文档是:


The document I'm bringing in the information from is:

8
7
000000011111S0000000110101111010101100010110111011010E00

<小时>

运行时得到的输出是 [[C@252f0999

请帮忙,我刚刚开始学习Java!

Help please, I'm just starting to learn java!

推荐答案

array 是一种特殊的对象,它没有隐含的 toString()管理数组内容的漂亮打印,结果是对象由对象的标准表示形式表示,即对象的哈希码.

array is a special kind of object, it doesn't have an implicit toString() which manages pretty printing of the content of the array, what happens is that the object is represented by the standard representation for objects which is its hashcode.

你应该使用 Arrays.toString():

You should use Arrays.toString():

for (int i = 0; i < array.length; ++i)
  System.out.println(Arrays.toString(array[i]));

请注意,您不能直接编写 Arrays.toString(array) 因为,如文档中所述:

Mind that you can't directly write Arrays.toString(array) because, as stated in documentation:

如果数组包含其他数组作为元素,它们会通过继承自 Object 的 Object.toString() 方法转换为字符串,该方法描述的是它们的身份而不是它们的内容.

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.

这篇关于二维数组,输出几乎不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆