当我尝试打印出一个矢量元素时,我得到了这些奇怪的字符! [英] I get these weird characters when I try to print out a vector element!

查看:200
本文介绍了当我尝试打印出一个矢量元素时,我得到了这些奇怪的字符!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Netbeans。
当我运行下面的程序时,我得到这个输出 [我@de6ced !为什么?

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

线程main中的异常java.lang.ClassCastException:[我无法在TopCoder.Test.main中强制转换为java.lang.Integer
(测试.java:13)
Java结果:1

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);
}


推荐答案

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.

所以看起来int数组被视为一个Object而不是一个整数数组。换句话说,自动拳击似乎不适用于它?

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?

这篇关于当我尝试打印出一个矢量元素时,我得到了这些奇怪的字符!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆