0

So I created three class called "Address", "Job" and "Person" with "Person" being my primary class. I test these by:

Address person2Address = new Address(1054, "Pico St", "Los Angeles", "CA", "97556");
    Address person2JobAddress = new Address(5435, "James St", "New York", "NY", "56565");
    ArrayList<String> person2Phone = new ArrayList<String>();
    person2Phone.add("555-555-55");
    Job person2Job = new Job("Mechanic", 35000.00, person2JobAddress);
    Person person2 = new Person("Rollan Tico", "New York", 'M', person2Address, person2Job, person2Phone);
    System.out.println(person2.toString());

They print everything correctly. Now, this is where I am stuck. How would I create a different class called Persons that stores each Person created in a ArrayList? Would there be any constrcunert? I know that an Arrayist is created by ArrayList<Person> List = new ArrayList<Person>();, but got a feeling I am missing something.

2
  • You don't need a separate class. Assuming you're looking for an immutable list, you can use List<Person> persons = Arrays.asList(person1, person2, ...); Commented Jan 27, 2017 at 3:04
  • @JacobG. Arrays.asList() is not immutable, just fixed-size. Commented Jan 27, 2017 at 3:20

2 Answers 2

1

You can have Collection like

Collection<Person> persons = new ArrayList<Person>(); persons.add(person2);

Or in some case, like JSON serialization you cannot seralize a list as the root element. so,

import java.util.*

public class Persons {

  private Collection<Person> persons;

  //If you want the clients to have flexibility to choose the implementation of persons collection.
  //Else, hide this constructor and create the persons collection in this class only.
  public Persons(Collection<Person> persons) {
    this.persons = persons;
  }

  public void addPerson(Person person) {
   persons.add(person);
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can't i pass arraylist directly :/ ? Can u elaborate please why did u use collection if one can do it with Arraylist as well @harivis
Yes, you can. List<Person> personList = new ArrayList<Person>(); personList.add(person1); //and so on.. Persons persons = new Persons(personList); You do not need this new class if you are well off with just having a Collection<Person> field.
ohk thanx :) so i don't need to type .add everytime to add new person if i use collectionlist ,right? @harivis +1
0

If you create a class for an object like Person, you don't need to create a Persons class just to store multiple Person objects. Unless you have to define operations which involves multiple Person Objects, like for example a Group class with operations performed on group pf persons, in which case creating a Persons or Group class makes sense. In your case I would assume you just need to store multiple Person objects and for that ArrayList<Person> will suffice.

ArrayList<Person> persons = new ArrayList<Person>();
persons.add(new Person(.....)); //add Person
.
.
Person person1=persons.get(1); //get Person by index

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.