Results 1 to 10 of 10
  1. #1
    lareauk is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default Can I store multiple objects in an array

    I am working on an array and want to store entries that will have multiple objects - for example a student, a student with a car, etc.

    I know the syntax to store one object, but not multiples.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    freedom exists in the world of ideas

  3. #3
    lareauk is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default Multiple object in an array

    I am new this this forum, so wasn't quite sure what or how to ask.

    I managed to solve this by storing objects within objects prior to storing within an array. For example, I put the car object (which contains brand, cost & miles) into the student object prior to putting into the array.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    M77's Avatar
    M77
    M77 is offline Member
    Join Date
    May 2008
    Location
    LV
    Posts
    43
    Rep Power
    0

    Default

    Object[] arr = new Object[10];
    arr[0]="Ordinary String";
    arr[1]=new Integer(77);
    System.out.println((String)arr[0]);
    if (arr[1] instanceof Integer) System.out.println(((Integer)arr[1]).toString());

  6. #6
    lareauk is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default Here is my solution

    I pass information to this method which can include just the student info or the student info and either a credit card object or a car object, which then gets stored in the array.

    //An array for holding students and their information
    //about credit cards and cars.
    protected Student[] studentCollection;

    //for holding the total number of items in Student array
    protected int manyItems = 0;

    /**
    * Method to create classroom based on size from tester
    */
    public ClassRoom (int size)
    {
    StudentCollection = new Student[size];
    }

    /**
    * Method to populate array with only Student information
    */
    public void addStudent (Student newStudent, int index)
    {
    studentCollection[index] = newStudent;
    manyItems++;
    }

    /**
    * Method to populate array with Student & CreditCard information
    */
    public void addStudent (Student newStudent, CreditCard newCreditCard,
    int index)
    {
    studentCollection[index] = newStudent;
    manyItems++;
    }

    /**
    * Method to populate array with Student & Car information
    */
    public void addStudent (Student newStudent, Car newCar, int index)
    {
    studentCollection[index] = newStudent;
    manyItems++;
    }

  7. #7
    gogoc is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default

    any array or any thing other then primitive types are objects in java so u can store any thing in an object array .
    get new coding problems at CODERWORDS

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  9. #9
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default

    you could store primitive too if you do new Integer(42)
    My IP address is 127.0.0.1

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 5
    Last Post: 07-30-2008, 09:30 AM
  2. Can I use vectors to store multiple types of objects
    By Nathand in forum Advanced Java
    Replies: 6
    Last Post: 04-28-2008, 07:55 AM
  3. Replies: 3
    Last Post: 04-07-2008, 06:02 PM
  4. questions about using array to store profile
    By hien_NU in forum New To Java
    Replies: 6
    Last Post: 01-08-2008, 05:03 AM

Posting Permissions

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