簡體   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