开发者

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 = ne开发者_如何学JAVAw 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?


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.


If you want to create many Home objects, each of which have a Sports and a School object, then you'll want to use an array or a collection class to store those objects. Let's assume you're using an array to store the Home objects, then it could look something like this:

// Array to store 10 Home objects
Home[] homes = new Home[10];

// Initialize each object in the array
for (int i = 0; i < homes.length; ++i) {
    homes[i] = new Home();

    // Create a Sports and a School object
    Sports sports = new Sports();
    School school = new School();

    // Add those to the i'th Home object
    homes[i].add(sports, school);
}

edit Or, do you want to create just one Home object and add multiple Sports and Schools object to it? Then it's even simpler:

Home home = new Home();

for (int i = 0; i < 10; ++i) {
    // Create a Sports and a School object
    Sports sports = new Sports();
    School school = new School();

    home.add(sports, school);
}


Create an array of Sports and School

Home h = new Home();
int count = 10;
Sports[] sports = new Sports[count];
School[] school = new School[count];
for (int i=0;i<count;i++)
{
sports[i] = new Sports();
school[i] = new School();
h.add(sports[i],school[i]);
}


Init an array of the required names and then use it to seed the Home instance.

Home h = new Home(); 
String[] names = new String[5]; 

// populate names here
// ...
// now populate Home
for (String name : names)
{     
    h.add(new Sports(name), new School(name));
} 


Do you mean something like this:

Home h = new Home();
while (CONDITION) {
   Sports t1 = new Sports();
   School s1 = new School();
   h.add(t1, s1);
}


Thank you for the answers, Do you think this is also a correct approach:

Sports s1;
School t2;
Home h = new Home();

for(int i=0; i<mysize; i++){
   s1 = new Sports("value");  //so i will change it as per the logic
   t2 = new School("value");
   h.add(s1, t2);
}

I am not sure though if this the right approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜