从Java的用户列表中移除用户。

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

Remove User from list of users in java

问题

我对Java还不熟悉,所以请原谅我的解释可能不够清楚,但我有一个列表:

  1. private LinkedList<User> users = new LinkedList<User>();

还有一个名为removeUser的方法:

  1. private void removeUser() {
  2. System.out.println("All Users: ");
  3. viewUsers();
  4. System.out.print("Please enter the ID of the user to be removed: ");
  5. int choice = In.nextInt();
  6. for (User user : users) {
  7. if (users.contains(choice)) {
  8. users.remove(choice);
  9. }
  10. }
  11. }

现在当我运行代码时,它成功地打印出了所有内容。但是当我检查列表内容时,什么也没有改变,我不确定我在代码的移除部分做错了什么。

英文:

I'm new to java, so please excuse me if my explanation is poor but I have a list :

  1. private LinkedList&lt;User&gt; users= new LinkedList&lt;User&gt;();

and a method removeUser:

  1. private void removeUser() {
  2. System.out.println(&quot;All Users: &quot;);
  3. viewUsers();
  4. System.out.print(&quot;Please enter the ID of the user to be removed: &quot;);
  5. int choice = In.nextInt();
  6. for (User user : users){
  7. if (users.contains(choice)) {
  8. users.remove(choice);
  9. }
  10. }
  11. }

Now when I run the code it successfully prints out everything. Yet when I check the list contents, nothing has changed and I'm unsure of what I have done wrong with the removal part of the code.

答案1

得分: 1

  1. 另一种解决方案如果你想使用 lambda 表达式和流来移除具有对应 ID 的第一个用户
  1. private void removeUser() {
  2. System.out.println("所有用户:");
  3. viewUsers();
  4. System.out.print("请输入要移除的用户的 ID:");
  5. int choice = In.nextInt();
  6. users.stream()
  7. .filter(user -> user.getId() == choice)
  8. .findFirst()
  9. .ifPresent(users::remove);
  10. }
英文:

Alternative solution: if you want to use lambda and streams to remove first User with corresponding ID.

  1. private void removeUser() {
  2. System.out.println(&quot;All Users: &quot;);
  3. viewUsers();
  4. System.out.print(&quot;Please enter the ID of the user to be removed: &quot;);
  5. int choice = In.nextInt();
  6. users.stream()
  7. .filter(user-&gt;user.getId() == choice)
  8. .findFirst()
  9. .ifPresent(users::remove);
  10. }

答案2

得分: -1

如果您想从列表中移除某个项目,您不能使用 for-each 循环。参考:https://stackoverflow.com/questions/33582050/what-is-a-difference-between-traditional-loop-and-for-each-loop

我认为您需要相同的操作:

  1. private void removeUser() {
  2. System.out.println("所有用户:");
  3. viewUsers();
  4. System.out.print("请输入要移除的用户的 ID:");
  5. int choice = In.nextInt();
  6. for (int i = 0; i < users.size(); i++){
  7. User user = users.get(i);
  8. if (user.contains(choice)) {
  9. users.remove(i); // 在索引 i 处移除
  10. }
  11. }
  12. }

希望能帮到您!

英文:

If you want remove some item from list you can't use for-each loop. Refer: https://stackoverflow.com/questions/33582050/what-is-a-difference-between-traditional-loop-and-for-each-loop

I think you need same:

  1. private void removeUser() {
  2. System.out.println(&quot;All Users: &quot;);
  3. viewUsers();
  4. System.out.print(&quot;Please enter the ID of the user to be removed: &quot;);
  5. int choice = In.nextInt();
  6. for (int i = 0; i &lt; users.size(); i++){
  7. User user = users.get(i);
  8. if (user.contains(choice)) {
  9. users.remove(i); // remove at index i
  10. }
  11. }
  12. }

Hope can help you!

huangapple
  • 本文由 发表于 2020年9月17日 10:25:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63930388.html
匿名

发表评论

匿名网友

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

确定