繁体   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");

    }
}

它甚至不会打印“这里?” 信息。 我认为这是一个无限循环,但是如果它甚至没有执行循环中的第一条指令,那么它怎么会陷入循环中呢? 我究竟做错了什么? 任何帮助将不胜感激。 它确实打印出了treeSet的预期大小。

编辑

test.txt的样本文件:

A B
B C
C D

无效的原因是while循环后的分号( ;

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

归结为:

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

第二部分( rest of the code )甚至不是循环的一部分,而是在循环之后执行。 通过删除分号,它将将修饰符( { } )之间的序列绑定到while指令。

结果,您根本不会在循环中调用.next()方法,因此会继续轮询是否存在下一个元素,但是由于您没有在迭代器中前进,因此总会有一个下一个元素。

最好不要在while循环后使用分号 ,甚至不要对以下一条指令使用分号

while(condition)
    instruction;

是的,这是有效的Java。 但是根据经验,这些东西最终会变得难以阅读。 一个更好的人总是使用赞誉,以明确表明您仅执行一条,多条指令或不执行任何指令。

暂无
暂无

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

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