java 在java中的for循环中初始化对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6625160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:41:27  来源:igfitidea点击:

Initializing objects in a for loop in java?

javaobjectinitialization

提问by Johnydep

Assuming Sports, School, Homeare classes in Java. with Homehaving a method e.g.

假设SportsSchoolHome在Java类。与Home具有例如方法

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 hremaining 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?

现在的问题是,我想重复这些步骤太多次。与h保持不变。因此,h.add()使用 forloop 将不同的对象作为参数调用多次,其中的值来自外部。有人可以告诉我如何初始化这些对象并在每次迭代中使用不同的对象名称在 e,ga for 循环中调用 add 方法吗?

回答by jjnguy

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 sportsand schoolsarrays.

在上面的示例中,您将需要一种方法来初始化sportsschools数组的内容。

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.

在这里,我们使用学校和运动名称数组来创建所有添加到Home.

回答by Jesper

If you want to create many Homeobjects, each of which have a Sportsand a Schoolobject, then you'll want to use an arrayor a collection classto store those objects. Let's assume you're using an array to store the Homeobjects, then it could look something like this:

如果你想创建许多Home对象,每个对象都有一个Sports和一个School对象,那么你需要使用一个数组或一个集合类来存储这些对象。假设您使用一个数组来存储Home对象,那么它可能看起来像这样:

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

editOr, do you want to create just one Homeobject and add multiple Sportsand Schoolsobject to it? Then it's even simpler:

编辑或者,你想只创建一个Home对象并添加多个SportsSchools对象呢?然后就更简单了:

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

回答by Steve Townsend

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

初始化一个包含所需名称的数组,然后使用它来为Home实例设定种子。

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

回答by Sherif elKhatib

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

回答by nfechner

Do you mean something like this:

你的意思是这样的:

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

回答by Johnydep

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.

我不确定这是否是正确的方法。