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

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

java.util.LinkedHashMap$LinkedKeyIterator infinite loop in LinkedHashSet

问题

System.out.print("输入链哈希集合的大小:");
Scanner s = new Scanner(System.in);  // 创建一个 Scanner 对象
int size = s.nextInt();
HashSet<String> lhashset = new HashSet<>(size);
for (int i = 0; i < size; i++) {
    System.out.print("输入姓名:");
    String name = s.next();
    lhashset.add(name);
}
System.out.println("\n输入你想查找的姓名:");
String find = s.next();
if (lhashset.contains(find)) {
    System.out.println("\n是的,链哈希集合包含" + find);
    System.out.print("你想要移除这个姓名吗?");
    String ch = s.next();
    String choice = "yes";
    if (ch.equals(choice)) {
        lhashset.remove(find);
        System.out.println("\n元素已移除。");
    } else {
        System.out.println("\n再见");
    }
} else {
    System.out.println("不包含这个姓名。");
}

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

英文:
System.out.print(&quot;Enter the size of linked hash set: &quot;);
	Scanner s = new Scanner(System.in);  // Create a Scanner object
	int size = s.nextInt();
		HashSet&lt;String&gt; lhashset = new HashSet&lt;&gt;(size);
	for(int i=0; i&lt;size; i++)
	{
		System.out.print(&quot;Enter the name: &quot;);
		String name = s.next();
		lhashset.add(name);
	}
		System.out.println(&quot;\nEnter the name you want to find: &quot;);
		String find = s.next();
		if(lhashset.contains(find))
		{       
    		System.out.println(&quot;\nYes the Linked Hash Set contains &quot; +find);
    		System.out.print(&quot;Do you want to remove the name? &quot;);
    		String ch = s.next();
		String choice = &quot;yes&quot;;
    		if(ch.equals(choice))
    		{
        			lhashset.remove(find);
        			System.out.println(&quot;\nElement removed.&quot;);
    		}
    		else
    		{
        			System.out.println(&quot;\nGoodbye&quot;);
    		}
		}
		else
		{
    		System.out.println(&quot;Does not contain name.&quot;);
		}

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

ch.equals(choice) 将无法工作 使用正确的语法更新代码

public class Soln {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);  // 创建一个 Scanner 对象
        HashSet<String> lhashset = new HashSet<>();
        lhashset.add("TEST");
        System.out.println("\n输入您想要查找的名称:");
        String find = s.next();
        if (lhashset.contains(find)) {       
            System.out.println("\n是的,Linked Hash Set 包含 " + find);
            System.out.print("您想要移除这个名称吗? ");
            int ch = s.nextInt();
            if (ch == 1) {
                lhashset.remove(find);
                System.out.println("\n元素已移除。");
            } else {
                System.out.println("\n再见");
            }
        } else {
            System.out.println("不包含该名称。");
        }
    }
}
英文:

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

public class Soln {
public static void main(String[] args) {
    Scanner s = new Scanner(System.in);  // Create a Scanner object
    HashSet&lt;String&gt; lhashset = new HashSet&lt;&gt;();
    lhashset.add(&quot;TEST&quot;);
    System.out.println(&quot;\nEnter the name you want to find: &quot;);
    String find = s.next();
    if(lhashset.contains(find))
    {       
        System.out.println(&quot;\nYes the Linked Hash Set contains &quot; +find);
        System.out.print(&quot;Do you want to remove the name? &quot;);
        int ch = s.nextInt();
        if(ch == 1)
        {
            lhashset.remove(find);
            System.out.println(&quot;\nElement removed.&quot;);
        }
        else
        {
            System.out.println(&quot;\nGoodbye&quot;);
        }
    }
    else
    {
        System.out.println(&quot;Does not contain name.&quot;);
    }
}
}

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

发表评论

匿名网友

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

确定