简体   繁体   English

在树集上使用迭代器的无限循环,而我正在调用next()方法

[英]Infinite loop using a iterator over a treeset and I'm invoking the next() method

import java.util.*;
import java.io.*;

public class Assignment5 {

    public static void main(String[] args) throws FileNotFoundException
    {
        File sets=new File("test.txt");             //create file to read

        HashMap<String, TreeSet<String>> hm=new HashMap<String, TreeSet<String>>();                   //create hashmap

        TreeSet<String> allNodes=new TreeSet<String>();             //create a treeset to hold
                                                    //all nodes. No duplicates
        Scanner in=new Scanner(sets);

        while(in.hasNext())                         //while file has content
        {                                           //keep scanning it

            String node=in.next();                  //first value in each line
            String edge=in.next();                  //refers to node. Second
                                                    //value refers to an edge
                                                    //of the node

            allNodes.add(node);                     //keep track of all nodes
            allNodes.add(edge);                     //we come across

            if(!hm.containsKey(node))               //if the node is not already
            {
                TreeSet<String> newTemp=new TreeSet<String>();
                newTemp.add(edge);                                    //in the hash map then we
                hm.put(node, newTemp);                  //need to add a key and
            }                                       //map its first value

            else                                    //if the node is already in
            {                                       //the hashmap then we need
                TreeSet<String> temp=(TreeSet<String>)hm.get(node); //just add the new edge to
                temp.add(edge);                     //it
                hm.put(node, temp);
            }
        }

                System.out.println(allNodes.size());

            //we now have a hash map containg any nodes that have an edge with
            //a treeset showing all edges from the node

            int count=0;                            //we go through the treemap
                                                    //and test if all nodes
            Iterator iter=allNodes.iterator();      //have an edge.  If a node
            while(iter.hasNext());                  //in the file does not have
            {
                System.out.println("here?");
                String theKey=(String)iter.next();          //an edge then it is a leaf
                if(hm.containsKey(theKey))
                {
                    count++;
                }
            }

        System.out.println("we made it here too");

    }
}

It won't even print the "here?" 它甚至不会打印“这里?” message. 信息。 I think it's an infinite loop but if it's not even executing the first instruction in the loop then how is it even getting stuck in the loop? 我认为这是一个无限循环,但是如果它甚至没有执行循环中的第一条指令,那么它怎么会陷入循环中呢? What am I doing wrong? 我究竟做错了什么? Any help would be greatly appreciated. 任何帮助将不胜感激。 It does print the expected size of the treeSet though. 它确实打印出了treeSet的预期大小。

EDIT : 编辑

A sample file for test.txt : test.txt的样本文件:

A B
B C
C D

The reason this doesn't work is because of the semicolon ( ; ) after the while loop: 无效的原因是while循环后的分号( ;

while(iter.hasNext());
{
    //The rest of the code
}

This boils down to: 归结为:

while(iter.hasNext()) {
    //no instructions
}
{
    //the rest of the code
}

The second part ( rest of the code ) is not even part of the loop, it is executed after the loop. 第二部分( rest of the code )甚至不是循环的一部分,而是在循环之后执行。 By removing the semicolon, it will bind the sequence between the accolades ( { } ) to the while instruction. 通过删除分号,它将将修饰符( { } )之间的序列绑定到while指令。

As a result you don't call the .next() method at all in the loop, and thus keep polling whether there is a next element, but since you don't advance in the iterator, there will always be a next element. 结果,您根本不会在循环中调用.next()方法,因此会继续轮询是否存在下一个元素,但是由于您没有在迭代器中前进,因此总会有一个下一个元素。

You better never use a semicolon after a while loop , not even for a single instruction like: 最好不要在while循环后使用分号 ,甚至不要对以下一条指令使用分号

while(condition)
    instruction;

Yes, this is valid Java. 是的,这是有效的Java。 But based on experience, these things tend to eventually become hard to read. 但是根据经验,这些东西最终会变得难以阅读。 One better always uses accolades, to make it explicit that you execute only one, more or no instructions. 一个更好的人总是使用赞誉,以明确表明您仅执行一条,多条指令或不执行任何指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM