This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes what syntax for getting instanceof 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 » Beginning Java
Reply Bookmark "what syntax for getting instanceof an array?" Watch "what syntax for getting instanceof an array?" New topic
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
    
  13

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
    
    9
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
    
    3

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: what syntax for getting instanceof an array?
 
Similar Threads
array doubt
Array reflection in Java
Multidim. array garbage collection
java.lang.ArrayIndexOutOfBoundsException - null
Two dimensional Vector