aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes converting Array Of Objects to Array Of Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "converting Array Of Objects to Array Of Strings" Watch "converting Array Of Objects to Array Of Strings" New topic
Author

converting Array Of Objects to Array Of Strings

Vivek Chaitanya
Ranch Hand

Joined: Oct 19, 2007
Posts: 35
Hi All

I have a HashMap with Key, Value pairs.I wanted to create a String array of Values in that HashMap.So I created a set of Values using keySet() method. Then I used toArray() method on the set.Now I have Array of Objects.

Now I looped through the object array and explicitly casted every object in that array to String.

Is this a correct way of doing this? If not, please suggest any other sophisticated way.

Thank you,
Chaitanya
Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
I think nothing's wrong with that. But here's a clearer way.

The toArray() method of a Set interface is overloaded, it has a version wich takes an initialized array as a parameter and puts all of the set members into this array. The array type must conform to the generic type of the set, so you won't need any type converting. Here's an example of using this method:

Gaurav Arora
Ranch Hand

Joined: Aug 13, 2007
Posts: 35
Serge's explanation is spot on but the code isn't correct.


The overloaded toArray method returns an array of the same type as it's argument. The docs will give you a better explanation of what I mean.
Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
Gaurav Arora, you can try my code, it compiles and runs perfectly well

Actually, it works both ways, but yours is a bit messier, because it creates a redundant object (new String[0]).

You can even say:

and this would also run fine. The return in this method is just for convenience.
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 49
Hello,

the code posted form Serge is correct, if you compile and run it you will see that it works. About the method toArray(T[]) API- DOC says:

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. 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.

And the String array allocated before is big enought to contain all the keys of the map, so it's not necessary to use the return type.


SCJP 6.0 [95%] OCP EJBD 6.0 [93%]
Gaurav Arora
Ranch Hand

Joined: Aug 13, 2007
Posts: 35
Mighty stupid of me to assume the code doesn't work based on the docs .. and misinterpreting them as well.

I take back my last post and my apologies Serge ...
Vivek Chaitanya
Ranch Hand

Joined: Oct 19, 2007
Posts: 35
Thank you all for letting me know the better way of doing what I want.

Thanks once again!!!

Chaitanya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: converting Array Of Objects to Array Of Strings
 
Similar Threads
Using Arrays
Stuck in a rut
Array of Objects to be passed into a method
Java equivalent for Perl's join ? ( concat Collection w/separator )
Casting Object[] into String[]