wood burning stoves 2.0*
The moose likes Java in General and the fly likes String[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "String[]" Watch "String[]" New topic
Author

String[]

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5300
    
  13

I have following code:


This code gives a ClassCastException on the bold line. So have i have to do a cast for each element?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
You can't do this conversion.

Use it in this way


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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]);


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5300
    
  13

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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
Bartender

Joined: Jul 19, 2004
Posts: 5300
    
  13

Thank you all for helping me out with this one. It's working great.

now it's weekend, so it's time to
 
GeeCON Prague 2014
 
subject: String[]