在for循环中如果找不到ID,则只打印一次。

huangapple go评论98阅读模式
英文:

Print only once in for loop if cannot find an ID

问题

我能够打印出一条消息,如果学生ID可用,但如果未找到ID,我只想要打印一次 println

以下是我删除学生的代码:

  1. public void removeStudent(int studentId) {
  2. for (int i = 0; i < students.size(); i++) {
  3. if (students.get(i).getId() != studentId) {
  4. continue;
  5. }
  6. System.out.println("已删除包含ID#: " + studentId + " 信息的档案。");
  7. this.students.remove(i);
  8. students = new ArrayList<>();
  9. return;
  10. }
  11. }
英文:

I was able to print a message if a student ID is available, but I want to only print a println once if the ID was not found.

below is my code for removing a student

  1. public void removeStudent(int studentId) {
  2. for (int i = 0; i &lt; students.size(); i++) {
  3. if (students.get(i).getId() != studentId) {
  4. continue;
  5. }
  6. System.out.println(&quot;Deleted a profile containing information for ID#: &quot; + studentId + &quot;.&quot;);
  7. this.students.remove(i);
  8. students = new ArrayList&lt;&gt;();
  9. return;
  10. }
  11. }

答案1

得分: 1

  1. 为什么你不在java.util.List中使用任何方法呢
  2. 我的意思是如果我必须编写一个删除一个或重复的id并且打印一些内容的方法我会写出类似这样的代码
  3. public void removeStudent(int studentId) {
  4. if (students.contains(studentId)) {
  5. students.removeIf(id -> id == studentId);
  6. System.out.println("已删除包含ID#的信息档案:" + studentId + "。");
  7. }
  8. else
  9. System.out.println("无内容可删除。");
  10. }
  11. 但是如果你不想改变你的代码太多你可以做类似这样的修改
  12. public void removeStudent(int studentId) {
  13. for (int i = 0; i < students.size(); i++) {
  14. if (students.get(i).getId() != studentId) {
  15. continue;
  16. }
  17. System.out.println("已删除包含ID#的信息档案:" + studentId + "。");
  18. this.students.remove(i);
  19. students = new ArrayList<>();
  20. return;
  21. }
  22. System.out.println("未找到ID。");
  23. }
  24. 我相当确定使用流stream可以使这个任务比这两段代码更顺畅
  25. 祝好
英文:

why do you not use any method in java.util.List ?
I mean, if I have to write a method who delete one (or duplicate) id and print something I would write something like this

  1. public void removeStudent(int studentId) {
  2. if( students.contains(studentId)) {
  3. students.removeIf(id -&gt; id==studentId);
  4. System.out.println(&quot;Deleted a profile containing information for ID#: &quot; + studentId + &quot;.&quot;);
  5. }
  6. else
  7. System.out.println(&quot;nothing to delete&quot;);
  8. }

But if you don't want to change your code to much you can just do something like this

  1. public void removeStudent(int studentId) {
  2. for (int i = 0; i &lt; students.size(); i++) {
  3. if (students.get(i).getId() != studentId) {
  4. continue;
  5. }
  6. System.out.println(&quot;Deleted a profile containing information for ID#: &quot; + studentId + &quot;.&quot;);
  7. this.students.remove(i);
  8. students = new ArrayList&lt;&gt;();
  9. return;
  10. }
  11. System.out.println(&quot; Id not found &quot;);
  12. }

And I'm pretty sure stream can make this job smoother than those 2 snippet

BR

答案2

得分: 0

基于 Tim 的回答,我建议移除 break 语句以删除所有具有相同 id 的学生,但仅打印一次:

  1. public boolean removeStudent(int studentId) {
  2. boolean removed = false;
  3. for (int i=0; i < students.size(); i++) {
  4. if (students.get(i).getId() == studentId) {
  5. System.out.println("已删除包含 ID# " + studentId + " 信息的档案。");
  6. this.students.remove(i);
  7. removed = true;
  8. }
  9. }
  10. return removed;
  11. }
英文:

Based on answer of Tim, I'd recommend remove the break to delete all students with the same id, but print only once:

  1. public boolean removeStudent(int studentId) {
  2. boolean removed = false;
  3. for (int i=0; i &lt; students.size(); i++) {
  4. if (students.get(i).getId() == studentId) {
  5. System.out.println(&quot;Deleted a profile containing information for ID#: &quot; + studentId + &quot;.&quot;);
  6. this.students.remove(i);
  7. removed = true;
  8. }
  9. }
  10. return removed;
  11. }

huangapple
  • 本文由 发表于 2020年9月1日 00:45:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63674869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定