File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes what syntax for getting instanceof an array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
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: 24184
    
  34

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: 3528
    
  15
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

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://aspose.com/file-tools
 
subject: what syntax for getting instanceof an array?