Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

converting an array of primitive types to array of strings.

Thread view: 
Anand Narasimhan - 11 Mar 2005 21:16 GMT
Hi,

Is there a way of converting an array of primitive types (or java
wrapper of primitive types) to array of strings without knowning what
type of primitive type the array consists of?

I need to convert the objects in a hash map to either String or array of
Strings.

Thanks
Anand
Eric Sosman - 11 Mar 2005 21:44 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> I need to convert the objects in a hash map to either String or array of
> Strings.

   If they're in a HashMap, either as keys or as values,
they're already objects of some kind and not primitives.
Every object has a toString() method (since Object itself
has one), which may or may not be satisfactory.

   If that's not what you're after (it might not be, as I
don't see where arrays enter the picture), please explain
more fully what you're trying to do.

Signature

Eric.Sosman@sun.com

Anand Narasimhan - 11 Mar 2005 23:42 GMT
Hi,

Thanks for the reply. Here is what I am trying to do.

I have a method "setInputParameters( HashMap map )". The values to the
HashMap can be a string, array of strings, arrays of primititve types
(int[], long[] etc).

Here is a sample usage scenario.

HashMap map = new HashMap();
int[] a = { 1, 2, 4, 5 };
String[] b = { "foo", "bar", "goo" };
String c = "xyz";
Integer d = new Integer( 10 );

map.put( "PARAM1", a );
map.put( "PARAM2", b );
map.put( "PARAM3", c );
map.put( "PARAM4", d );

FOO.setInputParameters( map );

In the setInputParameters I want to get these values as string array or
String depending on whethere the parameter is Object or an array of object.

I want to extract the infromation from the map and convert them as

String[] a = { "1", "2", "4", "5" };
String[] b = { "foo", "bar", "goo" };
String c = "xyz";
Stirng d = "10";

Hope this is clear.

Anand

>>Hi,
>>
[quoted text clipped - 13 lines]
> don't see where arrays enter the picture), please explain
> more fully what you're trying to do.
Eric Sosman - 14 Mar 2005 17:12 GMT
A: Reading in reverse too much like Yoda-speak is.
Q: What is the impediment?
A: Because it impedes understanding.
Q: Why is top-posting deprecated?

> [...]
> HashMap map = new HashMap();
[quoted text clipped - 16 lines]
> String c = "xyz";
> Stirng d = "10";

   Someone else may have a better solution, but all I can
think of is to fetch an Object from the Map, make a run-time
determination of its class (using instanceof and/or fancier
reflection techniques), and then process it according to
your whim.  This would take care of the values, but as far
as I can see there is no possibility of recovering the names
of the variables -- maybe you just used a,b,c,d twice by
coincidence, but if you had the intention of using `x42' to
plunk a value into the Map and then somehow magically plucking
a new `x42' back out of it, I think you're doomed.

   Overall, I don't think this approach is a good one.  The
general idea of object-oriented design is that the objects
implement their own behaviors; as soon as you start writing
"if it's a This do that(), else if it's an Other do ..." you
are separating the implementation from the object.  There are
circumstances where this makes sense, but if you find yourself
doing it frequently it may be time to step back and re-think
the design.

Signature

Eric.Sosman@sun.com

klynn47@comcast.net - 11 Mar 2005 22:01 GMT
Any object reference can be converted to a String with .toString
Anand Narasimhan - 12 Mar 2005 01:43 GMT
Yes. But if the object refers to  int[] the toString method returns
something like [I@197d257

Anand

> Any object reference can be converted to a String with .toString
Murray - 12 Mar 2005 04:00 GMT
> Hi,
>
[quoted text clipped - 7 lines]
> Thanks
> Anand

Iterate over the primitive array and call String#valueOf() to get the string
value


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.