Q:

Initializing objects in a for loop in java?

Assuming Sports, School, Home are classes in Java.
with Home having a method e.g.
void add(Sports t1, School t2);

Here is what i want to do:
Sports s1 = new Sports("value1");
School t2 = new School("value1");
Home h = new Home();
h.add(s1,t2);

Now the problem is, i want to repeat these steps too many times. with h remaining the same. So that h.add() is called multiple times with different objects as parameters with values coming from outside using forloop.
can someone show me how can i initialize these objects and call
the add method in e,g a for loop with different object names in
each iteration?

A:

Here is where you will want to use arrays (or lists).
Example:
Home h = new Home();
Sports[] sports = new Sports[10];
School[] schools = new School[10];
for (int i =0; i< sports.length; i++) {
    h.add(sports[i], schools[i]);
}

In the above example you will need a way to initialize the contents of the sports and schools arrays.
Perhaps this slight variation of above is what you need:
Home h = new Home();
String[] sports = // existing array of sport names
String[] schools = // array of school names
for (int i =0; i< sports.length; i++) {
    h.add(new Sports(sports[i]), new School(schools[i]));
}

Here we use arrays of school and sport names to create all of the objects that get added to Home.

