Results 1 to 3 of 3
  1. #1
    scheffetz is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default Multiple values of different types in an object

    I am new to java and I am having trouble figuring out if you can store multiple values of different types in an ArrayList object.

    For example, if I had a Person object and wanted to store their firstName, lastName, age, etc....

    Java Code:
    Public class Person {
    
         ArrayList <Person> person = new ArrayList <Person>();
    
        person.add("Joe", "Blogs", 33);
    
    }
    Can you store values like that?

    I do not know the method of how they would be added. My searches have come up empty.

    Would each object be considered a separate ArrayList?

  2. #2
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    102
    Rep Power
    0

    Default

    First of all, your Person class is invalid. If you are going to try out some code in your Person class, you should define a main() method and put your code there.

    Secondly, that is not the correct way to call the ArrayList add() method. You cannot do that. Look up the ArrayList API or examples online.

    Why don't you just create a Person object and store it in the ArrayList. You're halfway there. You just need to put some attributes/methods in your Person class. You could do something like this:
    Java Code:
    Person p1 = new Person("Joe", "Blogs", 33);
    Person p2 = new Person("Harry", "Houdini", 55);
    
    ArrayList <Person> alPerson = new ArrayList <Person>();
    alPerson.add(p1);
    alPerson.add(p2);
    Is that what you're trying to do?

  3. #3
    scheffetz is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Pass multiple object types to one function
    By desmo in forum New To Java
    Replies: 3
    Last Post: 10-08-2010, 07:51 AM
  2. Sorting/Searching Objects with multiple types.
    By gcampton in forum New To Java
    Replies: 20
    Last Post: 10-21-2009, 11:58 PM
  3. Multiple types in Vector - type checking
    By Excession in forum Advanced Java
    Replies: 5
    Last Post: 07-13-2008, 07:06 PM
  4. Hastable - Multiple Types
    By sopna sajith in forum Advanced Java
    Replies: 3
    Last Post: 06-29-2008, 04:40 AM
  5. 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

Posting Permissions

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