• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Need to find out the Class of Array Object

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I have a Object[][] array of (Integers,Strings,Double,Long etc).I just need to find out the class of each element type.I am trying something like this,which I am sure is wrong:

for(int i=0;i<noofrows;i++){
for(int j=0;j<noofcolumns;j++){
Class eType = array[i][j].getClass().getComponentType();
System.out.println("Type of Object["+i+"]["+j+"] :" + eType);
}
}
Can anyone please tell me where I am going wrong.Thanking you in advance.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well two problem's really: first use xxx.getClass().getName() and second use StringBuffer or MessageFormat to create your message string: concatentating strings inside two loops will slow things down a lot.
 
amrit choong
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Edward,
But when i do this,
String eType = array[i][j].getClass().getName();

It throws error:
array required, but java.lang.Object found.

please help.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use the instanceof operator:


This operator is often used with superclasses and subclasses to determine or validate if a class is a particular subclass. For example,



Now whether you should use the instanceof operator is a matter of discussion related to design and performance. Personally, I am not sure how "expensive" of a process instanceof is; perhaps others do...
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by amrit choong:
Thanks Edward,
But when i do this,
String eType = array[i][j].getClass().getName();

It throws error:
array required, but java.lang.Object found.

please help.



Can you post your full code, especially how you are declaring and populating your array, since the snippet you gave should work (and does work for me).
 
amrit choong
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks edward and mark,
But It's throwing the same error for both the methods.I am printing just for test purpose,so thats not an issue.Help me with this:
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue is with your printType method declaration:



It takes a paramater of type Object, not an array. Therefore your code array[i][j] is a syntax error since 'array' is of type Object, not an array of Objects. You can't access index values of an Object.

Change your method declaration to:

That will solve your issue since 'array' will now actually be an array.
[ January 10, 2005: Message edited by: Mark Vedder ]
 
amrit choong
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats why sometimes I think wat wud happen for ppl like me...if there was no javaranch....and no Edward's and Mark's..thanks guys
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, you are going to run into another issue since you have declared one of your array objects to be null (and that's actually a great thing to do for testing). You will however need to handle that situation with some Exception handling. Give it a try (no pun intended). If you have problems, post another question and someone can assist you.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mark Vedder:
Give it a try

Sweeeeeet.
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!