This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to find out the Class of Array Object  RSS feed

 
amrit choong
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.
 
Edward Kenworthy
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.
 
Mark Vedder
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.
 
David Harkness
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!