gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:05:43 25 4
gpt4 key购买 nike

我必须从外部文件中读取数字列表,并将它们插入到自己的数组中(正数或负数)。扫描仪读取数字的效果很好,但是当将它们插入数组时就会出错。正如您在下面看到的,是我的代码和输出。文件中的内容会打印在输出中,但是当我要求它打印数组时,它是困惑的字母/数字/符号。谁能帮我解决这个问题吗?

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

最佳答案

您将看到将数组直接转换为字符串的结果。数组也是对象,但它们不会覆盖 Object's toString()方法,它是造成“困惑”的原因。

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

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

使用 Arrays.toString 将数组内容显式转换为 String .

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

关于java - 可能的编码错误[I@24e11c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21863380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com