2

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?

2
  • 2
    It's not really clear what you mean. What's varying on each iteration of the loop? Note that variable names are very different from object names (objects don't have names). Commented Jul 8, 2011 at 13:33
  • Technically, they do have names (or at least a system identity, which is a name in a particular sense). You hardly ever want to know them though, especially since you can't look them up by name. Commented Jul 8, 2011 at 13:38

6 Answers 6

7

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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);
}

1 Comment

He clarified that Home stays constant in his problem.
1

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]);
}

1 Comment

it depends. in the code i gave you.. After the for loop, you will still have the 10 schools and sports.. If you dont want them any more then you only need a for loop like jjnguy said
1

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));
} 

2 Comments

Thank you but don't you think i will be using two loops when the task can be accomplished in just one loop.
If you want different values in the inputs to h.add then you need more logic than provided so far. If each h.add uses the same Sports and School name, then you are correct that only one loop is needed.
0

Do you mean something like this:

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

Comments

0

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.

2 Comments

value should be a variable that's given a value based on some condition of the for loop, rather than a String that says "value", but yes that should work.
@Anthony yes you are right. Actually these values will be coming in from JDBC connection as strings whcih i will add to those objects in this loop

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.