• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to detect if an object is an array  RSS feed

 
Mark Mescher
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the isArray() method of java.lang.Class too.



ram.
 
Mark Mescher
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!