有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java可能的编码错误[I@24e11c

我必须从一个外部文件中读取一个数字列表,然后将它们插入到自己的数组中,以确定它们是正的还是负的。扫描仪可以很好地读取数字,但当它将数字插入数组时,就会出错。如你所见,下面是我的代码和输出。文件中的内容会在输出中打印出来,但当我要求它打印数组时,却是那些杂乱无章的字母/数字/符号。谁能帮我修一下吗

public class Numbers {
    public static void main(String[] args) throws IOException {
        Scanner reader = new Scanner(new FileInputStream("First.dat"));
        int Positive[] = new int[20];
        int Negative[] = new int[20];
        int X = 0;
        int Y = 0;
        while (reader.hasNextInt()) {
            int Start = reader.nextInt();
            System.out.println(Start);
            if (Start < 0) {
                Negative[X] = Start;
                X += 1;
            }
            if (Start > 0) {
                Positive[Y] = Start;
                Y += 1;
            }

        }
        System.out.println(Positive + "  " + Negative);
    }
}

输出:

3
66
54
-8
22
-16
-56
19
21
34
-34
-22
-55
-3
-55
-76
64
55
9
39
54
33
-45
[I@1b41650  [I@24e11c

共 (1) 个答案

  1. # 1 楼答案

    您将看到将数组直接转换为String的结果。数组也是对象,但它们不会重写^{}'s ^{}方法,而^{}'s ^{}方法是造成“混乱”的原因

    In other words, this method returns a string equal to the value of:

    getClass().getName() + '@' + Integer.toHexString(hashCode())
    

    将数组的内容显式转换为带有^{}String

    System.out.println(Arrays.toString(Positive) + "  " + Arrays.toString(Negative));