当我尝试打印向量元素时,我得到这些奇怪的字符!

发布于 2024-07-30 02:30:59 字数 780 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我还不会笑 2024-08-06 02:30:59

看起来 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".

终陌 2024-08-06 02:30:59

[I@de6ced 可以分解为:
- [ 一个数组
- I 整数
- de6ced 带有此 reference 哈希码(在 Sun Java 中,基本上是引用)

toString() for Object 返回类似 ClassName@HashCode 的内容,这正是您在这里看到的只是(相当奇怪的)原始数组类的情况。

问题在于 推断出错误的类型。 列表asList(T...) 方法。 更改代码以使用 Integer[] 而不是 int[]。 这是 int 是原始类型的结果,但 int[] 是一个对象。

你可以直接看到这个:

System.out.println(Arrays.asList(new int[]{5}));

=> [[I@一些参考

System.out.println(Arrays.asList(new Integer[]{5}).get(0));

=> <代码>5

[I@de6ced can be broken down as:
- [ an array
- I of integers
- de6ced with this reference hash-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 use Integer[] instead of int[]. This is a consequence of int being primitive, but int[] is an object.

You can see this directly:

System.out.println(Arrays.asList(new int[]{5}));

=> [[I@some reference

System.out.println(Arrays.asList(new Integer[]{5}).get(0));

=> 5

瀟灑尐姊 2024-08-06 02:30:59
Integer[] a = new Integer[1];
a[0] = new Integer(5);
List list = Arrays.asList(a);
System.out.println(list.get(0));

上面的内容如您所期望的那样工作。

所以看起来“int”数组被视为一个对象,而不是一个整数数组。 换句话说自动装箱似乎不适用?

Integer[] a = new Integer[1];
a[0] = new Integer(5);
List list = Arrays.asList(a);
System.out.println(list.get(0));

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?

深海不蓝 2024-08-06 02:30:59

我想我已经弄清楚发生了什么:

 int[] a = new int[1];
 a[0] = 5;

我们现在有一个 int 数组。

 Vector<Integer> a1 = new Vector(Arrays.asList(a));

问题在于您调用 Arrays.asList 的方式。

asList 的签名是“public staticListasList(T... a)”。 它不适用于 T == int,因为 int 不是对象类型。 并且它无法与 T == Integer 匹配,因为数组 a 的基本类型是 int 而不是 Integer。 实际发生的情况是 T 绑定到 int[],并且 Arrays.aslist(a) 返回一个 Listint[]> ,其中一个元素是 a 的值!!!

然后,您从 List 创建一个 Vector 并获得一个带有一个元素的 Vector ...原始 int[] > 被分配给“a”。

 System.out.println(a1.elementAt(0));

最后,a1.elementAt(0) 获取 int[],最终调用 toString( 的 Object 实现) 方法。

从中可以学到一些重要的教训:

  • 混合原始类型和
    通用类型,就像你在网上所做的那样
    声明了a1
  • 忽略或转向是一个坏主意
    关闭编译器的泛型类型安全
    警告

I think I have figured out what was happening:

 int[] a = new int[1];
 a[0] = 5;

We now have an array of int.

 Vector<Integer> a1 = new Vector(Arrays.asList(a));

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 with T == int because int is not an object type. And it cannot match with T == Integer because the base type of the array a is int not Integer. What is actually happening is that T is binding to int[], and Arrays.aslist(a) is returning a List<int[]> with one element that is the value of a!!!

Then you create a Vector from the List and get a Vector with one element ... the original int[] that was assigned to 'a'.

 System.out.println(a1.elementAt(0));

Finally, a1.elementAt(0) fetches the int[], and you end up calling the Object implementation of the toString() method.

A couple of important lesson to learn from this:

  • it is a bad idea to mix raw types and
    generic types as you do on the line
    that declares a1, and
  • it is a bad idea to ignore, or turn
    off the compiler's generic type-safety
    warnings
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文