Results 1 to 4 of 4

Thread: Generic array

  1. #1
    eva
    eva is offline Member
    Join Date
    Dec 2007
    Posts
    49
    Rep Power
    0

    Default Generic array

    I have an array and want to make it generic. Its size is 10 and I want it to either contain int values or string values. Array type depends on the user's selection at runtime.

    Is this possible?

    - PEACE

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    762
    Rep Power
    10

    Default

    You can create an object array. If you use the auto boxing feature you can just put any object in the array.

    Java Code:
    Object[] data = new Object[2];
    data[0] = 10;
    data[1] = "hello";
    Is this help you?

  3. #3
    Join Date
    Dec 2007
    Posts
    24
    Rep Power
    0

    Default

    or u can use the arraylist man..then u can put any type of variable or any type of object into that oone thats useful 2 u i think..


    public class Testing
    {

    public static void main(String args[])
    {
    Integer i1=new Integer(45);//Integer object
    Double d1=new Double(45.567);//double object
    String s1=new String("agsd");//string object
    Calc c1=new Calc();//object of the class
    ArrayList a1= new ArrayList();
    a1.add("45");// u can insert direct values also like this
    a1.add("a");
    a1.add("4de5");
    a1.add(45);
    a1.add(45.56);
    a1.add('a');
    a1.add('4');
    a1.add("45.79");
    a1.add(i1);a1.add(d1);a1.add(s1);
    a1.add(c1);

    System.out.println(""+a1);


    }
    }

  4. #4
    eva
    eva is offline Member
    Join Date
    Dec 2007
    Posts
    49
    Rep Power
    0

    Default

    So I can either use ArrayList or use an object array. Thanks for the details.

    - PEACE

Similar Threads

  1. Replies: 0
    Last Post: 04-07-2008, 08:14 PM
  2. can anyone help... 2d Array
    By Mark1989 in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 09:59 PM
  3. Writing generic methods
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 04:28 AM
  4. Generic Hashtables
    By ShoeNinja in forum New To Java
    Replies: 0
    Last Post: 12-04-2007, 11:43 PM
  5. Generic Eclipse Modeling System 3.0 RC1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-25-2007, 04:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •