multiple datatype array, how can I create one?
I want to create an array element that store multiple data types, for example
I'm using Object[] mult_type = new Object[4];
How can I go about to do that in storing different type of objects and retrieve
the objects back into it's original states?
data type index
--------------------
String 0
int 1
long 2
short 3
........ ..
Re: multiple datatype array, how can I create one?
First, you can only store Objects in an array of Objects. This means
everything except the primitive types (int, char, boolean, etc.); if you
want to store them you have to wrap them in their corresponding Object
Wrapper classes (Integer, Character, Boolean, etc.) So for your examples:
mult_type[0] = "A String";
mult_type[1] = new Integer(42);
mult_type[2] = new Long(7149994000);
and so on. Although mult_type[i] is an Object by definition, the entry
stored there can be any subclass of Object. When you want to retrieve them,
you can examine them to find out what class they actually belong to. There
are a couple of ways to do this, one is to use the "instanceof" operator
like so:
if (mult_type[i] instanceof Integer) {
Integer anInteger = (Integer)mult_type[i];
int anInt = anInteger.intValue();
}
Notice that you have to "cast" the object to its actual class as you
retrieve it.
PC2
Ming <ming.fang@sungard> wrote in message news:39ecb84a$1@news.devx.com...
>
> I want to create an array element that store multiple data types, for
example
> I'm using Object[] mult_type = new Object[4];
> How can I go about to do that in storing different type of objects and
retrieve
> the objects back into it's original states?
>
>
> data type index
> --------------------
> String 0
> int 1
> long 2
> short 3
> ....... ..
Re: multiple datatype array, how can I create one?
The Map of OBJECT and associated TYPE might work for these kind of requirements.
"Ming" <ming.fang@sungard> wrote:
>
>I want to create an array element that store multiple data types, for example
>I'm using Object[] mult_type = new Object[4];
>How can I go about to do that in storing different type of objects and retrieve
>the objects back into it's original states?
>
>
>data type index
>--------------------
>String 0
>int 1
>long 2
>short 3
>........ ..