• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I convert an ArrasyList of type String to a STring array?

Thanks for your help!
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you see something in the ArrayList API that may help?

Henry
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 76870
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The link Fred quoted appears to assign the array reference twice, so it is not a particularly elegant solution.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
John Zwick
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys!
I ended up using:

 
Campbell Ritchie
Marshal
Posts: 76870
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks a nice bit of code.
 
Rancher
Posts: 4327
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 4327
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 76870
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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()]".
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic