This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String[]

 
Roel De Nijs
Sheriff
Posts: 10386
132
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?
 
David Ulicny
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
 
Peter Chase
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 ]
 
Stan James
(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]);
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
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: 10386
132
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
Marshal
Pie
Posts: 24212
35
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: 10386
132
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