java - 过多的迭代使数据结构变得困惑

标签 java arraylist io

我希望我的输出如下所示:

/home/flavius/data/train/politics/p_0.txt, [L'Etat,, c'est, moi.]
/home/flavius/data/train/science/s_0.txt, [If, I, have, seen, further, it, is, by, standing, on, the, shoulders, of, giants.]
/home/flavius/data/train/atheism/a_0.txt, [Gott, ist, tot.]
/home/flavius/data/train/sports/s_1.txt, [You, miss, 100%, of, the, shots, you, don't, take.]

但是此时,它看起来像下面这样,前面附加了四行:

/home/flavius/data/train/atheism/a_0.txt
/home/flavius/data/train/politics/p_0.txt
/home/flavius/data/train/science/s_0.txt
/home/flavius/data/train/sports/s_1.txt
/home/flavius/data/train/politics/p_0.txt, [L'Etat,, c'est, moi.]
/home/flavius/data/train/science/s_0.txt, [If, I, have, seen, further, it, is, by, standing, on, the, shoulders, of, giants.]
/home/flavius/data/train/atheism/a_0.txt, [Gott, ist, tot.]
/home/flavius/data/train/sports/s_1.txt, [You, miss, 100%, of, the, shots, you, don't, take.]

我的问题是,为什么要添加前四行?

程序读取四个目录下的不同文件,然后为每个文件在 HashMap 中创建一个条目,以文件名作为键,并将该文件中包含的所有单词存储为数组列表。

这就是代码,非常简单。也许有人能发现我哪里出错了。

public class FileDictCreateur 
{
    static String PATH = "/home/flavius/data/train";

    static Map<File, ArrayList<String> > fileDict = new HashMap<>();

    public static void main(String[] args) throws IOException 
    {
        //each of the diferent categories
        String[] categories = { "/atheism", "/politics", "/science", "/sports"};

        //cycle through all categories once to populate the global dict
        for(int cycle = 0; cycle <= 3; cycle++)
        {
            String general_data_partition = PATH + categories[cycle];

            File directory = new File( general_data_partition );
            iterateDirectory( directory );  
        }

        for (Map.Entry entry : fileDict.entrySet()) 
        {
            System.out.println(entry.getKey() + ", " + entry.getValue());
        }
    }

    private static void iterateDirectory(File directory) throws IOException 
    {
        for (File file : directory.listFiles()) 
        {
            if (file.isDirectory()) 
            {
                iterateDirectory(directory);
            } 
            else 
            {
                System.out.println(file);

                String line; 
                BufferedReader br = new BufferedReader(new FileReader( file ));

                while ((line = br.readLine()) != null) 
                {
                    String[] words = line.split(" ");//those are your words

                    //populate_globo_dict(words);

                    create_file_dict( file, words );

                }
            }
        }
    }

    public static void create_file_dict( File file, String[] words ) throws IOException
    {   

        if (!fileDict.containsKey(file))
        {
            ArrayList document_words = new ArrayList<String>();

            String word;

            for (int i = 0; i < words.length; i++) 
            {
                word = words[i];

                document_words.add(word);
            }
            fileDict.put(file, document_words);
        }
    }
}

最佳答案

从此 for 循环:

for (int cycle = 0; cycle <= 3; cycle++) {
        String general_data_partition = PATH + categories[cycle];
        File directory = new File(general_data_partition);
        System.out.println(directory);
        iterateDirectory(directory);
 }

删除以下语句:System.out.println(directory);,该语句打印目录名称。

关于java - 过多的迭代使数据结构变得困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28555642/

相关文章:

java - 使用 ArrayList 内部的引用来调用方法,并更改引用对象的当前状态?

java - 这些代码有内存泄漏吗?

java - Java 中的可变长度(动态)数组

javascript - 如何在javascript中将列表转换为多个参数

c# - 从文本文件中读取并更新

java - 尝试读取 jar 中的文件时出错

c - 在 C 中正确操作宽字符/字符串

java - 为什么我的代码可以在某些网站上运行,但不能在 NetBeans 中运行?

java - spring-data-jdbc 只读

Java - 混合数据类型/打印它们