I get these weird characters when I try to print out a vector element!

前端 未结 4 751
青春惊慌失措
青春惊慌失措 2021-01-21 15:29

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;

publ         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-21 16:33

    [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 List 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

提交回复
热议问题