英文:
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 < students.size(); i++) {
       if (students.get(i).getId() != studentId) {
                continue;
       }
       System.out.println("Deleted a profile containing information for ID#: " + studentId + ".");
       this.students.remove(i);
       students = new ArrayList<>();
       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 -> id==studentId);
	    	System.out.println("Deleted a profile containing information for ID#: " + studentId + ".");
    	    }
    	else 
            System.out.println("nothing to delete");
    }
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 < students.size(); i++) {
       if (students.get(i).getId() != studentId) {
                continue;
       }
       System.out.println("Deleted a profile containing information for ID#: " + studentId + ".");
       this.students.remove(i);
       students = new ArrayList<>();
       return;
  }
  System.out.println(" Id not found ");
}
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 < students.size(); i++) {
        if (students.get(i).getId() == studentId) {
            System.out.println("Deleted a profile containing information for ID#: " + studentId + ".");
            this.students.remove(i);
            removed = true;
        }
    }
    return removed;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论