• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

String[]  RSS feed

 
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have following code:


This code gives a ClassCastException on the bold line. So have i have to do a cast for each element?
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do this conversion.

Use it in this way
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection.toArray() returns Object[], even if all the elements of the Collection happen to be castable to a narrower type.

Collection.toArray(Object[]) is declared to return Object[], but the run-time type of the returned array is the same as the run-time type of the array given as an argument.

In your case, if you construct an array of type String[] and pass that in, then the returned array will be of run-time type String[] and your cast will then succeed.

The String[] that you construct can be of length zero, in which case Java will automatically construct and return a new array of the correct length. Alternatively, you can construct your String[] array with the correct length (using Collection.size() to see what that is); Java will then use that array and return it.

Messy, but it does work and you get used to it.


In Java 5.0, you can use generics to make a Collection that stores only String objects. Its toArray() method will then automatically return the correct type.
[ February 18, 2005: Message edited by: Peter Chase ]
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The size of the array in toArray argument does not matter, just the type. I use it like this all the time:

String[] x = (String[])list.toArray(new String[0]);
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Stan James:
The size of the array in toArray argument does not matter, just the type.



Maybe it's a micro-optimization, but note that if the array is the right size, it will be used, whereas if it's not, a new one of the right type needs to be allocated. Therefore, if you have to allocate a 0-element array, you might as well allocate one of the right size and save an allocation.

Both IntelliJ IDEA and Eclipse come with an intention action built-in which lets you type the whole thing, including allocating the array at the proper size, with just a few keystrokes.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:


Maybe it's a micro-optimization, but note that if the array is the right size, it will be used, whereas if it's not, a new one of the right type needs to be allocated. Therefore, if you have to allocate a 0-element array, you might as well allocate one of the right size and save an allocation.

Both IntelliJ IDEA and Eclipse come with an intention action built-in which lets you type the whole thing, including allocating the array at the proper size, with just a few keystrokes.



Could you tell me which keystrokes in Eclipse i have to use?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's see. In Eclipse, you type "toarray", and press Ctrl-? (or whatever you've got completion bound to.) The little template that appears lets you supply the type for the array and the variable name for the collection, sometimes with the correct variable name already filled in.

In IDEA, you type "toar" and press TAB. IDEA seems to be considerably smarter about figuring out the type and variable name.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for helping me out with this one. It's working great.

now it's weekend, so it's time to
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!