Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 7th, 2005, 12:21 PM   #1
swmk
Newbie
 
Join Date: Oct 2005
Posts: 19
Rep Power: 0 swmk is on a distinguished road
Exclamation Getting type for Array

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
swmk is offline   Reply With Quote
Old Oct 7th, 2005, 12:27 PM   #2
swmk
Newbie
 
Join Date: Oct 2005
Posts: 19
Rep Power: 0 swmk is on a distinguished road
Oops, what's wrong with my browser. I got duplicate posts.
swmk is offline   Reply With Quote
Old Oct 7th, 2005, 1:01 PM   #3
peace_of_mind
Professional Programmer
 
peace_of_mind's Avatar
 
Join Date: Sep 2004
Location: Hell if I know most of the time
Posts: 439
Rep Power: 5 peace_of_mind is on a distinguished road
Send a message via MSN to peace_of_mind Send a message via Yahoo to peace_of_mind
Must've hit refresh right after sumbitting or something. Reported to mod for removal.
__________________
Amateurs built the ark
Professionals built the Titanic

peace_of_mind is offline   Reply With Quote
Old Oct 7th, 2005, 4:35 PM   #4
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
Quote:
Originally Posted by swmk
Oops, what's wrong with my browser. I got duplicate posts.
I removed the duplicate ... you may want to use the [ code ][ /code ] tags next time you post a snippet of code ... it makes it a lot easier for others to read.
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
/* Don't make me use it! */
sykkn is offline   Reply With Quote
Old Oct 8th, 2005, 1:40 AM   #5
thenewkid
Programmer
 
Join Date: Sep 2004
Location: New Jersey
Posts: 36
Rep Power: 0 thenewkid is on a distinguished road
Send a message via AIM to thenewkid
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.
thenewkid is offline   Reply With Quote
Old Oct 8th, 2005, 5:46 AM   #6
swmk
Newbie
 
Join Date: Oct 2005
Posts: 19
Rep Power: 0 swmk is on a distinguished road
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
swmk is offline   Reply With Quote
Old Oct 16th, 2005, 6:51 AM   #7
ersken
Newbie
 
Join Date: Oct 2005
Posts: 2
Rep Power: 0 ersken is on a distinguished road
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) {
..
ersken is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC