Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用并行ArrayList从数组创建直方图_Java_Arrays_Arraylist_Histogram - Fatal编程技术网

Java 使用并行ArrayList从数组创建直方图

Java 使用并行ArrayList从数组创建直方图,java,arrays,arraylist,histogram,Java,Arrays,Arraylist,Histogram,我写了一个学习ArrayList的程序。本质上,它所做的是从一个字符串数组(每个字符串都是一个单词)中提取,并使用并行数组列表查找重复项。有两个数组列表,一个用于单词,另一个用于每个单词出现的次数。单词列表第0点中的单词对应于计数列表第0点中的数字,依此类推。我成功地找到了重复的单词并计算了它们的出现次数,但是对于只出现一次的单词,我得到了2的计数,我似乎无法找到原因。这是密码 String[] wordList = fileContents.split("\\s"); Arr

我写了一个学习ArrayList的程序。本质上,它所做的是从一个字符串数组(每个字符串都是一个单词)中提取,并使用并行数组列表查找重复项。有两个数组列表,一个用于单词,另一个用于每个单词出现的次数。单词列表第0点中的单词对应于计数列表第0点中的数字,依此类推。我成功地找到了重复的单词并计算了它们的出现次数,但是对于只出现一次的单词,我得到了2的计数,我似乎无法找到原因。这是密码

String[] wordList = fileContents.split("\\s");

        ArrayList<String> words = new ArrayList<String>();
        ArrayList<Integer> counts = new ArrayList<Integer>();
        words.add(wordList[0]);
        counts.add(0);
        for (int i = 0; i < wordList.length; i++) {
            String tempWord = wordList[i];
            boolean foundEqual = false;
            int count = 0;
            for(int q = 0;q < words.size();q++) {
                if (tempWord.equals(words.get(q))) {
                    foundEqual = true;
                    counts.add(q, counts.get(q) + 1);
                } 

            }
            if(!foundEqual){
                words.add(tempWord);
                counts.add(1);
            }

        }
        for (int i = 0; i < words.size(); i++) {
            System.out.println(words.get(i) + ":" + counts.get(i));
        }
这里是输出,正如你所看到的,最后三项应该是1,但是是2

this:3
is:3
a:2
test:3
also:2
the:2
last:2

任何帮助都将不胜感激

旁白:你的主循环应该从
i=1
开始,因为
i=0
在循环开始之前就被覆盖了

错误在于,您正在计算每个后续行程的第一次发生次数。将
q=0
更改为
q=i+1
,以避免出现这种情况


因此,您还必须检查结束条件。

旁白:您的主循环应该在
i=1
开始,因为
i=0
在循环开始之前已被覆盖

错误在于,您正在计算每个后续行程的第一次发生次数。将
q=0
更改为
q=i+1
,以避免出现这种情况


因此,您还必须检查结束条件。

在打印语句之前查看调试器中的
计数
单词
的结构时,很明显有问题

words: counts 0 = this 0 = 3 1 = is 1 = 3 2 = a 2 = 2 3 = test 3 = 3 4 = also 4 = 2 5 = the 5 = 2 6 = last 6 = 2 7 = 1 8 = 0 9 = 1 10 = 1 11 = 1 12 = 1 13 = 1 14 = 1 这是以下内容的第一位:

    words.add(wordList[0]);
    counts.add(0);
当它再次点击for循环时:

words: counts 0 = this 0 = 1 1 = 0 然后再次匹配“this”:

words: counts 0 = this 0 = 2 1 = is 1 = 1 2 = a 2 = 0 3 = test 3 = 1 4 = 1 5 = 1 关键词:计数 0=这0=2 1=等于1=1 2=a 2=0 3=测试3=1 4 = 1 5 = 1
您应该能够看到这种结构是如何不正确地增长的。

在print语句之前,在调试器中查看
计数
单词的结构时,很明显有问题

words: counts 0 = this 0 = 3 1 = is 1 = 3 2 = a 2 = 2 3 = test 3 = 3 4 = also 4 = 2 5 = the 5 = 2 6 = last 6 = 2 7 = 1 8 = 0 9 = 1 10 = 1 11 = 1 12 = 1 13 = 1 14 = 1 这是以下内容的第一位:

    words.add(wordList[0]);
    counts.add(0);
当它再次点击for循环时:

words: counts 0 = this 0 = 1 1 = 0 然后再次匹配“this”:

words: counts 0 = this 0 = 2 1 = is 1 = 1 2 = a 2 = 0 3 = test 3 = 1 4 = 1 5 = 1 关键词:计数 0=这0=2 1=等于1=1 2=a 2=0 3=测试3=1 4 = 1 5 = 1
您应该能够看到这种结构是如何错误地增长的。

非常感谢!慢慢地越来越擅长捕捉那些小东西!有不止一个bug,因为@MichaelT的观点也是正确的。每次添加
时,您都必须
删除
。非常感谢!慢慢地越来越擅长捕捉那些小东西!有不止一个bug,因为@MichaelT的观点也是正确的。每次添加
,您都必须
删除
。另外,您真的应该使用此代码来研究使用关联数组(在Java中称为映射)来存储字符串=>整数关系,而不是使用双数组列表。这将是一次很好的学习经历。上面的解决方案解决了它,但我对你的很好奇。你说我在列表中添加了一个新元素,因此所有内容都被删除了,但是根据我在最新版本的java(或更新版本)中的理解,他们更改了它,因此当你指定一个索引时,它只对先前存在的索引起作用。也许我不明白你在说什么。谢谢你的帮助!另一方面,您确实应该使用这段代码来研究使用关联数组(在Java中称为Map)的情况,该数组允许您存储String=>Integer关系,而不是双数组列表。这将是一次很好的学习经历。上面的解决方案解决了它,但我对你的很好奇。你说我在列表中添加了一个新元素,因此所有内容都被删除了,但是根据我在最新版本的java(或更新版本)中的理解,他们更改了它,因此当你指定一个索引时,它只对先前存在的索引起作用。也许我不明白你在说什么。谢谢你的帮助!