This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes How to convert ArrayList<String> to String[]? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to convert ArrayList<String> to String[]?" Watch "How to convert ArrayList<String> to String[]?" New topic
Author

How to convert ArrayList<String> to String[]?

John Zwick
Ranch Hand

Joined: Mar 02, 2009
Posts: 34
How can I convert an ArrasyList of type String to a STring array?

Thanks for your help!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40

Did you see something in the ArrayList API that may help?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Don't want to cut the rug out from under Henry, who is a top notch mod. But I really struggled with this question, and the Sun documentation didn't make it easy. Google helped though.

http://www.java2s.com/Code/JavaAPI/java.util/ArrayListtoArrayTa.htm
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

It's rather amateurish to re-invent the wheel when built-in methods already exist. Spending about 30 seconds with the java.util.ArrayList javadoc answers the question.

Not trying to pick on you, but I don't see how it could be any easier.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
The link Fred quoted appears to assign the array reference twice, so it is not a particularly elegant solution.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Hate to burst your bubble, but it is.

Collection.toArray(T[]) will return the array you passed to it if it is large enough. In short, the following is done (pseudo code):
By actually passing in an array that already has length size() you make sure the array itself is large enough, so it will be returned.
If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Zwick
Ranch Hand

Joined: Mar 02, 2009
Posts: 34
Thanks guys!
I ended up using:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
That looks a nice bit of code.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
Rob Prime wrote:Hate to burst your bubble, but it is.

I believe Campbell was referring to the code shown in the link:

And I agree, it's inelegant to assign twice; it's unnecessary, and suggests a misunderstanding of how toArray() works. The code John found later is nicer. Alternately, this also eliminates the double assignment:

Or:

And in fact, the presence of these multiple, seemingly contradictory paradigms suggests to me that the original API design of toArray() is less clear than it might have been. It's a pity that Java generics were implemented as they are; a non-erasable implementation might have allowed us to do something nice and simple like

That would have been elegant. Oh well.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Mike Simmons wrote:

That's horrible. You create not one but two arrays here, the first one only to determine the runtime type of the array to return. Talk about a waste of memory.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
Yes, because zero-length arrays with no reference retained are soooo wasteful. Your threshold for "horrible" seems quite a bit lower than mine. Whatever.

More importantly: I wasn't actually endorsing that code; I was just pointing out that there were multiple usage patterns, leading to user confusion on how to use that method. Which is, IMO, bad. But I would also note that of all the possible usage patterns, that's the one that is actually endorsed, specifically, by )]the official API. And yet it's not, in my opinion, very elegant. So I'm kind of at a loss, trying to figure out what it is you actually have an issue with. In my opinion, the API is poorly designed. It's possible to use it elegantly, but also inelegantly, and the latter is extremely common. That's not my fault, nor Campbell's fault - it's Sun's. So why are you busting our chops instead?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

I guess it's just a matter of differences in style. I like to avoid creating objects you throw away immediately. I usually go for the "toArray(new Integer[c.size()])" solution, but when that would make the line too long I will use a temp variable to increase readability.

Let's drop this, shall we? In the end the code examples both you and John provided all work like a charm, so it's up to the programmer to decide which one he/she prefers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
What about the no-args toArray() method and cast it to String[]? No you can't; I tried and got a ClassCastException.I ran it several times. Oddly enough most of the time it appears significantly faster to pass a zero-length array to the toArray method.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Campbell Ritchie wrote:What about the no-args toArray() method and cast it to String[]? No you can't; I tried and got a ClassCastException.

That's because the object really is an Object[] - as in "new Object[size()]".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert ArrayList<String> to String[]?