multiple datatype array, how can I create one?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: multiple datatype array, how can I create one?

  1. #1
    Ming Guest

    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
    ........ ..

  2. #2
    Paul Clapham Guest

    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
    > ....... ..




  3. #3
    ShuPu Guest

    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
    >........ ..



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center