| Author |
what syntax for getting instanceof an array?
|
Chris Jones
Greenhorn
Joined: Feb 18, 2008
Posts: 12
|
|
I want to write a conditional expression that checks if an object is an array. Can I use instanceof for this? How is the syntax? I imagine something along these lines: // code fragment: but eh ... that's evidently not how things are, so what do I instead need to do to determine type of an array object? all help much appreciated!
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
|
Actually, your code snippet is fine (given that it appears inside a method, of course.) That's exactly the right thing to do. Maybe whatever you actually tried was a little bit different from this?
|
[Jess in Action][AskingGoodQuestions]
|
 |
qingwu wang
Ranch Hand
Joined: Feb 19, 2003
Posts: 147
|
|
I don't think it is a good way to solve your question. only as a referrence. try{ String str = someArray.getClass().getName(); if(str!=null){ if(str.index("[")==0){ System.out.println("This is an array"); } } }catch(e){ }
|
Thanks...qingwu<br />When I open my eyes,I see your pretty face.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Originally posted by Chris Jones:
If you're not worried about what type of array it is you could do
|
Joanne
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12928
|
|
You can try something different, as qingwu and Joanne show, but still: the syntax you are trying to use is correct and should work. Have a good look at your source code again. Isn't there something else you overlooked? Maybe a missing brace or semi-colon? Try copying and pasting this example, and compiling it: it works without errors.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: what syntax for getting instanceof an array?
|
|
|