| Author |
Using Lists and converting them to arrays problem
|
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
i have the following problems:
1. why is the output of line 25
[[I@3e25a5] ???
2.when the 30th line is commented out,i get the following error:
\Jcreator FIles\Test3.java:38: incompatible types
found : java.lang.Object[]
required: java.lang.Integer[]
is there anyway by which i can cast the returned object to integer ?
3.The line 52 output is
[sadf, df, 34, Test3@addbf1]
why is the output [sadf, df, 34, Test3@addbf1] and not as that of List list2fromarray2 output [[I@3e25a5] ?
|
OCPJP 6.0 93%
OCPJWCD 5.0 98%
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
mohitkumar gupta wrote:
1. why is the output of line 25
[[I@3e25a5] ???
2.when the 30th line is commented out,i get the following error:
\Jcreator FIles\Test3.java:38: incompatible types
found : java.lang.Object[]
required: java.lang.Integer[]
is there anyway by which i can cast the returned object to integer ?
3.why is the output [sadf, df, 34, Test3@addbf1] and not as that of List list2fromarray2 output [[I@3e25a5] ?
1. int[] direct subclass of Object from which it inherit toString method
2.toArray of List returns Object[] so cast as (Integer[])objArray
3.unlike array Object, Integer and String overrides toString() to give meaningful String representation
hth
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
1. int[] direct subclass of Object from which it inherit toString method
2.toArray of List returns Object[] so cast as (Integer[])objArray
3.unlike array Object, Integer and String overrides toString() to give meaningful String representation
1.list2fromarray2 and listoldstyle are both lists that can hold any type of value,
when used inside System.out.println they should print the contents of the lists.
2.when listoldstyle is used inside System.out.println ,it prints the list contents.
while list2fromarray2 made from an array which hold int values,when used inside System.out.println , prints list hashcode.
why such different behavior ?
As far i know list override toString method,so when used inside System.out.println the output should be the list contents
3.The toArray() is of 2 formats.one that returns an array object and other that returns an array of type passed as it's call argument.In this case aa .
the code at line 27-29 works fine but code at line 30 requires casting.
The only difference is the list from which array is being made is different.
code at line 27 uses list2fromarray1(can hold only Integer values)
code at line 30 uses list2fromarray2 that is made from array2(array which holds only int values)
why such a behaviour ?
i didn't get your answer Seetharaman.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16680
|
|
mohitkumar gupta wrote:
1. int[] direct subclass of Object from which it inherit toString method
1. list2fromarray2 and listoldstyle are both lists that can hold any type of value,
when used inside System.out.println they should print the contents of the lists.
No. This is not true. Remember that the collections hold Objects -- and primitive types are not objects. The reason it seems to be able to hold any type is because autoboxing is doing the conversion for you. It just looks like any type, but it is not.
And autoboxing has its limits. The asList() method takes a var-arg of Objects. And while autoboxing can convert a parameter list of primative types to a parameter list of wrapper objects, it is not able to box an array of primatives being passed. Instead, the var-arg will simply take it as a single object, which is the array itself.
This is why the printout looks so weird, it is showing a single element -- and the single element is an array ("[") or primitive type integer ("I") with a particular identity hashcode ("@xxxxx").
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16680
|
|
mohitkumar gupta wrote:
2.toArray of List returns Object[] so cast as (Integer[])objArray
2.when listoldstyle is used inside System.out.println ,it prints the list contents.
while list2fromarray2 made from an array which hold int values,when used inside System.out.println , prints list hashcode.
why such different behavior ?
As far i know list override toString method,so when used inside System.out.println the output should be the list contents
It's not a different behavior -- its a different list. The contents of the lists are different. See my previous post.
The asList() method returns a list, with a single element, and the element is the int array.
Henry
|
 |
 |
|
|
subject: Using Lists and converting them to arrays problem
|
|
|