aspose file tools
The moose likes Java in General and the fly likes How to detect if an object is an array Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to detect if an object is an array" Watch "How to detect if an object is an array" New topic
Author

How to detect if an object is an array

Mark Mescher
Ranch Hand

Joined: Oct 25, 2004
Posts: 34
Hi,
if I have e.g. a hashmap is there a possibility to check if the Object behind a specific name is an array?
In my software a HashMap can also contain Stringobjects and Stringarrays so I have a problem when using
String s=(String)hash.get(name);
if there is an array.
Any solutions for this?
Thx.
Mark
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You can always get things out of a Collection as an Object, and you can find out what an Object is with the "instanceof" operator. So:

Both lines of output will return true.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

You could use the isArray() method of java.lang.Class too.



ram.
Mark Mescher
Ranch Hand

Joined: Oct 25, 2004
Posts: 34
Ok Thank you both solutions work for me.
I thought a String Array would be also an Object Array and not an Object.I was surprised that instanceof works here:-)
Bye
Mark
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to detect if an object is an array
 
Similar Threads
Mapping Question
Setting ResultSet into an ArryList
converting Array Of Objects to Array Of Strings
string[] modification
hashmap