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

标签 java treeset

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
}

第二部分(其余代码)甚至不是循环的一部分,它在循环之后执行。通过删除分号,它将把荣誉之间的序列 ({ }) 绑定(bind)到 while 指令。

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

你最好在 while 循环后永远不要使用分号,即使对于像这样的单个指令也不要使用:

while(condition)
    instruction;

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

关于java - 在树集上使用迭代器的无限循环,我正在调用 next() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29686981/

相关文章:

java - 更新struts2中迭代器标签的值

java - OpenCV HoughLines 只返回一行

java - 为什么 TreeSet 在添加新元素之前不比较所有元素?

java - 了解Android的webview addjavascriptinterface

java - 如何使用 Eclipse 作为编辑器在 Maven 项目中查找和丢失 Artifact ?

java - 如何在二叉树中找到下一个顺序后继者?

Java按频率排序的非重复列表

java - 如何打印 TreeSet 中的对象

java - 不解密我加密的东西

java - 为什么HashSet加null不抛出异常,而TreeSet加null会抛出异常