• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Using Lists and converting them to arrays problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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] ?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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.

 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Grow a forest with seedballs and this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic