java.util.LinkedHashMap$LinkedKeyIterator在LinkedHashSet中导致无限循环。

huangapple 未分类评论54阅读模式
英文:

java.util.LinkedHashMap$LinkedKeyIterator infinite loop in LinkedHashSet

问题

  1. System.out.print("输入链哈希集合的大小:");
  2. Scanner s = new Scanner(System.in); // 创建一个 Scanner 对象
  3. int size = s.nextInt();
  4. HashSet<String> lhashset = new HashSet<>(size);
  5. for (int i = 0; i < size; i++) {
  6. System.out.print("输入姓名:");
  7. String name = s.next();
  8. lhashset.add(name);
  9. }
  10. System.out.println("\n输入你想查找的姓名:");
  11. String find = s.next();
  12. if (lhashset.contains(find)) {
  13. System.out.println("\n是的,链哈希集合包含" + find);
  14. System.out.print("你想要移除这个姓名吗?");
  15. String ch = s.next();
  16. String choice = "yes";
  17. if (ch.equals(choice)) {
  18. lhashset.remove(find);
  19. System.out.println("\n元素已移除。");
  20. } else {
  21. System.out.println("\n再见");
  22. }
  23. } else {
  24. System.out.println("不包含这个姓名。");
  25. }

第一个 if 语句正常工作,但是当我尝试进入 else 语句(打印"不包含")时,根据标题的描述,我陷入了无限循环。对于嵌套的 if 语句也是一样的。

英文:
  1. System.out.print(&quot;Enter the size of linked hash set: &quot;);
  2. Scanner s = new Scanner(System.in); // Create a Scanner object
  3. int size = s.nextInt();
  4. HashSet&lt;String&gt; lhashset = new HashSet&lt;&gt;(size);
  5. for(int i=0; i&lt;size; i++)
  6. {
  7. System.out.print(&quot;Enter the name: &quot;);
  8. String name = s.next();
  9. lhashset.add(name);
  10. }
  11. System.out.println(&quot;\nEnter the name you want to find: &quot;);
  12. String find = s.next();
  13. if(lhashset.contains(find))
  14. {
  15. System.out.println(&quot;\nYes the Linked Hash Set contains &quot; +find);
  16. System.out.print(&quot;Do you want to remove the name? &quot;);
  17. String ch = s.next();
  18. String choice = &quot;yes&quot;;
  19. if(ch.equals(choice))
  20. {
  21. lhashset.remove(find);
  22. System.out.println(&quot;\nElement removed.&quot;);
  23. }
  24. else
  25. {
  26. System.out.println(&quot;\nGoodbye&quot;);
  27. }
  28. }
  29. else
  30. {
  31. System.out.println(&quot;Does not contain name.&quot;);
  32. }

The first if statement works fine, but when I try to go to else statement(print"does not contain"), I get an infinite loop as per the heading. The same happens for the nested if statement.

答案1

得分: 0

  1. ch.equals(choice) 将无法工作 使用正确的语法更新代码
  2. public class Soln {
  3. public static void main(String[] args) {
  4. Scanner s = new Scanner(System.in); // 创建一个 Scanner 对象
  5. HashSet<String> lhashset = new HashSet<>();
  6. lhashset.add("TEST");
  7. System.out.println("\n输入您想要查找的名称:");
  8. String find = s.next();
  9. if (lhashset.contains(find)) {
  10. System.out.println("\n是的,Linked Hash Set 包含 " + find);
  11. System.out.print("您想要移除这个名称吗? ");
  12. int ch = s.nextInt();
  13. if (ch == 1) {
  14. lhashset.remove(find);
  15. System.out.println("\n元素已移除。");
  16. } else {
  17. System.out.println("\n再见");
  18. }
  19. } else {
  20. System.out.println("不包含该名称。");
  21. }
  22. }
  23. }
英文:

ch.equals(choice) will not work. Updating the code with correct syntax.

  1. public class Soln {
  2. public static void main(String[] args) {
  3. Scanner s = new Scanner(System.in); // Create a Scanner object
  4. HashSet&lt;String&gt; lhashset = new HashSet&lt;&gt;();
  5. lhashset.add(&quot;TEST&quot;);
  6. System.out.println(&quot;\nEnter the name you want to find: &quot;);
  7. String find = s.next();
  8. if(lhashset.contains(find))
  9. {
  10. System.out.println(&quot;\nYes the Linked Hash Set contains &quot; +find);
  11. System.out.print(&quot;Do you want to remove the name? &quot;);
  12. int ch = s.nextInt();
  13. if(ch == 1)
  14. {
  15. lhashset.remove(find);
  16. System.out.println(&quot;\nElement removed.&quot;);
  17. }
  18. else
  19. {
  20. System.out.println(&quot;\nGoodbye&quot;);
  21. }
  22. }
  23. else
  24. {
  25. System.out.println(&quot;Does not contain name.&quot;);
  26. }
  27. }
  28. }

huangapple
  • 本文由 发表于 2020年4月6日 02:42:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/61047674.html
匿名

发表评论

匿名网友

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

确定