| 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
|
 |
 |
|
|
subject: How to detect if an object is an array
|
|
|