![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 19
Rep Power: 0
![]() |
Hi,
I got no idea how to get the type in Array. E.g if(parameters.isPrimitive()) { /* Code Goes here for Primitive */ } else if ( parameters.isArray() ) { /* * Then, I need to find out what types are in that array * I'm stucked here. */ } Can anyone help me with this? Regards; Stephen |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Oct 2005
Posts: 19
Rep Power: 0
![]() |
Oops, what's wrong with my browser. I got duplicate posts.
|
|
|
|
|
|
#3 |
|
Professional Programmer
|
Must've hit refresh right after sumbitting or something. Reported to mod for removal.
__________________
Amateurs built the ark Professionals built the Titanic |
|
|
|
|
|
#4 | |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
Quote:
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ]; /* Don't make me use it! */ |
|
|
|
|
|
|
#5 |
|
Programmer
|
if the array is the array of some objects, as far as i know if you are looking for the name of the class from which the object was instantiated you could simply use this: some_array[0].getClass().getName()..you can look those methods up in javadoc online at: http://java.sun.com/j2se/1.3/docs/api/index.html..hope this helps.
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Oct 2005
Posts: 19
Rep Power: 0
![]() |
some_array[0].getClass().getName() --
Not like that I don't even know what is the length of the array. I only know the parameter is array. and getName() will be OK if the array are of Objects type and getName() will return something weird if it is primitive. But, I've found a way. getComponentType() returns the true type. If short, it will return short. Regards; Stephen |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Oct 2005
Posts: 2
Rep Power: 0
![]() |
instanceof
The easiest way would be to use instanceof.
If you want know the type of the array, do like this: myObject myArray[] = new myObject[10]; if (myArray instanceof myObject[]) { .... If you need to find out the type of one element in the array:. Animal animals[] = new animals[10]; animals[0] = new Cat(); animals[1] = new Dog(); if (animals[0] instanceof Cat) { .. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|