二维数组,输出接近正确的任何地方 [英] 2D Array, output is no where near correct

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

问题描述

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!

推荐答案

阵列是一个特殊的对象,它没有一个隐含的的toString()管理​​该阵列的内容pretty的印刷,会发生什么情况是,对象重新$ P $的对象psented的标准重新presentation这是它哈希code。

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.

您应该使用<一个href="http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#toString%28java.lang.Object%5B%5D%29"><$c$c>Arrays.toString():

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

记住,你不能直接写 Arrays.toString(阵列),因为,正如文件中指出:

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

如果数组包含其他数组作为元素,它们是由Object.toString()方法从Object继承,这说明他们的身份,而不是它们的内容转换为字符串。

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天全站免登陆