当我尝试打印向量元素时,我得到这些奇怪的字符!
我正在使用 Netbeans。 当我运行下面的程序时,我将其作为输出 [I@de6ced
! 怎么会?
import java.util.Arrays;
import java.util.Vector;
public class Test {
public static void main (String[] args) {
int[] a = new int[1];
a[0] = 5;
Vector<Integer> a1 = new Vector(Arrays.asList(a));
System.out.println(a1.elementAt(0));
}
}
我也尝试解决它,但后来我
在线程“main”java.lang.ClassCastException中遇到异常:[我无法转换为java.lang.Integer 在 TopCoder.Test.main(Test.java:13) Java 结果:1
public static void main (String[] args) {
int[] a = new int[1];
a[0] = 5;
Vector<Integer> a1 = new Vector(Arrays.asList(a));
int b = a1.elementAt(0); /* EXCEPTION THROWN HERE */
System.out.println(b);
}
I'm using Netbeans.
When I run the program below, I get this as output [I@de6ced
! How come?
import java.util.Arrays;
import java.util.Vector;
public class Test {
public static void main (String[] args) {
int[] a = new int[1];
a[0] = 5;
Vector<Integer> a1 = new Vector(Arrays.asList(a));
System.out.println(a1.elementAt(0));
}
}
I also tried working around it but then I got a
Exception in thread "main" java.lang.ClassCastException: [I cannot be cast to java.lang.Integer
at TopCoder.Test.main(Test.java:13)
Java Result: 1
public static void main (String[] args) {
int[] a = new int[1];
a[0] = 5;
Vector<Integer> a1 = new Vector(Arrays.asList(a));
int b = a1.elementAt(0); /* EXCEPTION THROWN HERE */
System.out.println(b);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来 int 正在使用自动装箱变成整数,因此您将获得对象引用而不是值。 仍然看起来很奇怪,因为它应该调用正确的 toString 并以“5”结尾。
It looks like the int's are becoming Integers using autoboxing so you are getting an object reference instead of the value. Still seems weird as it should call the correct toString and end up with "5".
[I@de6ced
可以分解为:-
[
一个数组-
I
整数-
de6ced
带有此reference哈希码(在 Sun Java 中,基本上是引用)toString() for Object 返回类似 ClassName@HashCode 的内容,这正是您在这里看到的只是(相当奇怪的)原始数组类的情况。
问题在于
推断出错误的类型。 列表asList(T...)
方法。 更改代码以使用Integer[]
而不是int[]
。 这是 int 是原始类型的结果,但 int[] 是一个对象。你可以直接看到这个:
=>
[[I@
一些参考=> <代码>5
[I@de6ced
can be broken down as:-
[
an array-
I
of integers-
de6ced
with thisreferencehash-code (which, in Sun Java, is basically the reference)toString() for Object returns somethine like ClassName@HashCode, which is exactly what you're seeing happen here just with the (rather wierd) primitive-array classes.
The problem is that the wrong type is being inferred by the
<T> List<T> asList(T...)
method. Change your code to useInteger[]
instead ofint[]
. This is a consequence of int being primitive, but int[] is an object.You can see this directly:
=>
[[I@
some reference=>
5
上面的内容如您所期望的那样工作。
所以看起来“int”数组被视为一个对象,而不是一个整数数组。 换句话说自动装箱似乎不适用?
The above works as you would expect.
So it looks like the "int" array is treated like an Object and not an array of Integers. In other words auto boxing doesn't seem to be applied to it?
我想我已经弄清楚发生了什么:
我们现在有一个 int 数组。
问题在于您调用 Arrays.asList 的方式。
asList 的签名是“
public staticListasList(T... a)
”。 它不适用于T == int
,因为int
不是对象类型。 并且它无法与T == Integer
匹配,因为数组a
的基本类型是int
而不是Integer
。 实际发生的情况是T
绑定到int[]
,并且Arrays.aslist(a)
返回一个List
int[]>
,其中一个元素是a
的值!!!然后,您从
List
创建一个Vector
并获得一个带有一个元素的Vector
...原始int[]
> 被分配给“a”。最后,
a1.elementAt(0)
获取int[]
,最终调用toString( 的
方法。Object
实现)从中可以学到一些重要的教训:
通用类型,就像你在网上所做的那样
声明了
a1
,关闭编译器的泛型类型安全
警告
I think I have figured out what was happening:
We now have an array of int.
The problem is in the way you are calling
Arrays.asList
.The signature for asList is "
public static <T> List<T> asList(T... a)
". It does not apply withT == int
becauseint
is not an object type. And it cannot match withT == Integer
because the base type of the arraya
isint
notInteger
. What is actually happening is thatT
is binding toint[]
, andArrays.aslist(a)
is returning aList<int[]>
with one element that is the value ofa
!!!Then you create a
Vector
from theList
and get aVector
with one element ... the originalint[]
that was assigned to 'a'.Finally,
a1.elementAt(0)
fetches theint[]
, and you end up calling theObject
implementation of thetoString()
method.A couple of important lesson to learn from this:
generic types as you do on the line
that declares
a1
, andoff the compiler's generic type-safety
warnings