• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collection.toArray Problem

 
Ranch Hand
Posts: 686
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my declaration:
SelectedAsset selectedAsset[]= null;
Collection selectedAssetsCollection;

in the method I am calling follwing code.

selectedAsset = new SelectedAsset[selectedAssetsCollection.size()];
selectedAsset=(SelectedAsset[])selectedAssetsCollection.toArray();

when I checked selectedAssetsCollection.size() returns me size 1.
But it gives me classcast exception, while it contains object of type SelectedAsset.

I wonder for this awkword behave with toArray method.
 
Jignesh Patel
Ranch Hand
Posts: 686
Mac
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this is the work around with the problem.
selectedAsset=(SelectedAsset[])selectedAssetsCollection.toArray(selectedAsset);

But why simple toArray() method is not working. contradictory of toArray(Object[]);
[ September 08, 2005: Message edited by: Jignesh Patel ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that toArray() is declared to return Object[]. Also, the underlying collection has no way of knowing what the concrete types for its contents are. You should look at the overloaded version of toArray() that takes an array parameter. If you have problems, use the search tool to find previous discussions here or in the beginner forum.

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jignesh Patel:
I found this is the work around with the problem.
selectedAsset=(SelectedAsset[])selectedAssetsCollection.toArray(selectedAsset);

But why simple toArray() method is not working. contradictory of toArray(Object[]);

[ September 08, 2005: Message edited by: Jignesh Patel ]



In your orignal version you weren't using toArray(Object[]) because you weren't sending a parameter to it.

Layne
reply
    Bookmark Topic Watch Topic
  • New Topic