如何在每次循环中创建一个新对象?(How can I create a new object every pass through a loop?)

编程入门 行业动态 更新时间:2024-10-28 10:31:50
如何在每次循环中创建一个新对象?(How can I create a new object every pass through a loop?)

我正在做一个项目,其中可能有任何数量的对象我可能需要创建,我永远不会确切知道我需要做多少。

我想要做的是每次通过这个循环时都创建一个新对象。 然后它会立即将它添加到名为list的arrayList中。

for (int x = 0; x < list.size(); x++) { GradeInfo person(x) = new GradeInfo(); list.add(person(x)); list.get(x).setStudentID(inputFile.nextInt()); inputFile.nextLine(); list.get(x).setFullName(inputFile.nextLine()); testScore = inputFile.nextDouble(); list.get(x).setTestScore(testScore); list.get(x).setGrade(testScore); averageScore = averageScore + testScore; }

以上是我每次通过循环时尝试创建一个新对象,但它不起作用。

理想的情况是

GradeInfo person1 = new GradeInfo(); list.add(person1);

这种情况会发生并且会发生多次通过循环,在我的情况下它会创建10个人对象。 任何帮助将不胜感激,谢谢。

I'm doing a project where there may be any number of objects that I might need to create and I'll never know for sure exactly how many I have to make.

What I want to do is create a new object every time there is a new pass through this loop. Then it will immediately add it to the arrayList called list.

for (int x = 0; x < list.size(); x++) { GradeInfo person(x) = new GradeInfo(); list.add(person(x)); list.get(x).setStudentID(inputFile.nextInt()); inputFile.nextLine(); list.get(x).setFullName(inputFile.nextLine()); testScore = inputFile.nextDouble(); list.get(x).setTestScore(testScore); list.get(x).setGrade(testScore); averageScore = averageScore + testScore; }

The above is my attempt at making a new object every time it passes the loop, but it isn't working.

The ideal scenario would be

GradeInfo person1 = new GradeInfo(); list.add(person1);

that this occurs and happens for how ever many times it goes through the loop, in my case it would create 10 person objects. Any help would be appreciated, thanks.

最满意答案

你发布的例子是否编译? 此语法不是有效的Java:

GradeInfo person(x) = new GradeInfo();

看起来你混合了一些C ++。 但是你有正确的想法:在循环的每次迭代中创建一个新对象......只需在循环的每次迭代中创建一个新对象!

for (int x = 0; x < list.size(); x++) { GradeInfo person = new GradeInfo(); list.add(person); ... }

但是,添加到当前正在迭代的列表中是不明智的,特别是对于评估for循环结束条件的方式。 编写它的方式,每次迭代都会在列表中添加一些内容,然后在迭代结束时,将再次调用list.size() ,它将返回列表的新大小。 所以这实际上最终会成为一个无限循环,因为你的列表不断扩展。 所以你应该在这里使用两个单独的列表: GradeInfo和其中一个人。

您也不需要检索刚刚添加到列表中的GradeInfo以便使用它; 您已经有了它的引用,因此您可以直接对引用执行操作,您将看到已添加到列表中的项目中反映的更改。

int numPersons = personList.size(); List<GradeInfo> gradeInfos = new ArrayList<GradeInfo>(); for (int x = 0; x < numPersons; x++) { GradeInfo newGradeInfo = new GradeInfo(); gradeInfos.add(newGradeInfo); newGradeInfo.setStudentID(inputFile.nextInt()); inputFile.nextLine(); newGradeInfo.setFullName(inputFile.nextLine()); testScore = inputFile.nextDouble(); newGradeInfo.setTestScore(testScore); newGradeInfo.setGrade(testScore); averageScore = averageScore + testScore; }

您将得到一个不同GradeInfo对象的列表。 如果您要编写一个获取GradeInfo并打印出其信息的方法,您可以遍历此列表并查看确切的结果。

for (final GradeInfo gradeInfo : gradeInfos) { printInfo(gradeInfo); } => StudentID: 123 FullName: asdf asdf TestScore: 100 Grade: 100 ---------------------- StudentID: 456 FullName: michael jackson TestScore: 23 Grade: 23 ---------------------- ....

Does your posted example compile? This syntax is not valid Java:

GradeInfo person(x) = new GradeInfo();

It looks like you are mixing in a bit of C++. You've got the right idea, though: to create a new object at each iteration of a loop...simply create a new object at each iteration of the loop!

for (int x = 0; x < list.size(); x++) { GradeInfo person = new GradeInfo(); list.add(person); ... }

However, it is not wise to add to the list you are currently iterating over, especially with the way you are evaluating your for loop's end condition. The way this is written, each iteration will add something to your list, then at the end of the iteration list.size() will be called again and it will return the new size of the list. So this will actually end up being an infinite loop, because your list is constantly expanding. So you should really be working with two separate lists here: one of GradeInfo, and one of persons.

You also do not need to retrieve the GradeInfo you've just added to the list in order to work with it; you already have a reference to it, so you can act directly on the reference and you will see the changes reflected in the item you've added to the list.

int numPersons = personList.size(); List<GradeInfo> gradeInfos = new ArrayList<GradeInfo>(); for (int x = 0; x < numPersons; x++) { GradeInfo newGradeInfo = new GradeInfo(); gradeInfos.add(newGradeInfo); newGradeInfo.setStudentID(inputFile.nextInt()); inputFile.nextLine(); newGradeInfo.setFullName(inputFile.nextLine()); testScore = inputFile.nextDouble(); newGradeInfo.setTestScore(testScore); newGradeInfo.setGrade(testScore); averageScore = averageScore + testScore; }

You will end up with a list of distinct GradeInfo objects. If you were to write a method that takes a GradeInfo and prints out its information, you could loop over this list and see exactly what happens.

for (final GradeInfo gradeInfo : gradeInfos) { printInfo(gradeInfo); } => StudentID: 123 FullName: asdf asdf TestScore: 100 Grade: 100 ---------------------- StudentID: 456 FullName: michael jackson TestScore: 23 Grade: 23 ---------------------- ....

更多推荐

本文发布于:2023-07-29 23:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1320789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建一个   对象   如何在   create   pass

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!