• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collection.toArray Problem  RSS feed

 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • 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: 626
Mac
  • Mark post as helpful
  • send pies
  • 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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!