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

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

Print only once in for loop if cannot find an ID

问题

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

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

public void removeStudent(int studentId) {

   for (int i = 0; i < students.size(); i++) {
       if (students.get(i).getId() != studentId) {
                continue;
       }
       System.out.println("已删除包含ID#: " + studentId + " 信息的档案。");
       this.students.remove(i);
       students = new ArrayList<>();
       return;
  }
}
英文:

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

public void removeStudent(int studentId) {

   for (int i = 0; i &lt; students.size(); i++) {
       if (students.get(i).getId() != studentId) {
                continue;
       }
       System.out.println(&quot;Deleted a profile containing information for ID#: &quot; + studentId + &quot;.&quot;);
       this.students.remove(i);
       students = new ArrayList&lt;&gt;();
       return;
  }
}

答案1

得分: 1

为什么你不在java.util.List中使用任何方法呢
我的意思是如果我必须编写一个删除一个或重复的id并且打印一些内容的方法我会写出类似这样的代码

    public void removeStudent(int studentId) {
        if (students.contains(studentId)) {
            students.removeIf(id -> id == studentId);
            System.out.println("已删除包含ID#的信息档案:" + studentId + "。");
        }
        else 
            System.out.println("无内容可删除。");
    }

但是如果你不想改变你的代码太多你可以做类似这样的修改

    public void removeStudent(int studentId) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId() != studentId) {
                continue;
            }
            System.out.println("已删除包含ID#的信息档案:" + studentId + "。");
            this.students.remove(i);
            students = new ArrayList<>();
            return;
        }
        System.out.println("未找到ID。");
    }

我相当确定使用流stream可以使这个任务比这两段代码更顺畅

祝好
英文:

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

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

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

public void removeStudent(int studentId) {

   for (int i = 0; i &lt; students.size(); i++) {
       if (students.get(i).getId() != studentId) {
                continue;
       }
       System.out.println(&quot;Deleted a profile containing information for ID#: &quot; + studentId + &quot;.&quot;);
       this.students.remove(i);
       students = new ArrayList&lt;&gt;();
       return;
  }
  System.out.println(&quot; Id not found &quot;);
}

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

BR

答案2

得分: 0

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

public boolean removeStudent(int studentId) {
    boolean removed = false;
    for (int i=0; i < students.size(); i++) {
        if (students.get(i).getId() == studentId) {
            System.out.println("已删除包含 ID# " + studentId + " 信息的档案。");
            this.students.remove(i);
            removed = true;
        }
    }

    return removed;
}
英文:

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

public boolean removeStudent(int studentId) {
    boolean removed = false;
    for (int i=0; i &lt; students.size(); i++) {
        if (students.get(i).getId() == studentId) {
            System.out.println(&quot;Deleted a profile containing information for ID#: &quot; + studentId + &quot;.&quot;);
            this.students.remove(i);
            removed = true;
        }
    }

    return removed;
}

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:

确定