• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

toArray()

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody explain me difference bwtween two versions of toArray()
I tried a lot...i readJava docs
but could nt really findout
thanku
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume you're talking about a Collection (Set or List).

The no-args version returns an array of Object references (which is how elements are stored in collections anyway). So to use something in this array, you will probably need to downcast the reference back to its original type.

The overridden version uses generics to return an array of the specified type. This automatically downcasts the Object references so that they can be immediately used without explicit downcasting.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc, you said the overloaded version ,toArray(Object[] ) when used so ,automatically downcasted, no explicit casting required.
But i tried a program and it show error with expplicit casting ,and even i read it from some web link ,that Still Explicit casting is required s compiler doesnt know about method's argument.

I couldnt understand the need and use of 2nd version of toArray().
The output is same,Ofcourse.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example.
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic