OutOfMemoryError:Java堆空间 - ArrayLists Java

时间:2014-05-14 15:14:33

标签: java arraylist out-of-memory

    for(int i=0; i<words.size(); i++){
        for(int j=0; j<Final.size(); j++){
        if(words.get(i)==Final.get(j)){
            temp=times.get(j);
            temp=temp+1;
            times.set(j, temp);         
        }
        else{
            Final.add(words.get(i));
            times.add(1);
        }   
        }

}

我想创建两个ArrayLists; times(整数)和Final(String)。 ArrayList“words”包括字符串的单词,并且一些单词被多次显示。我想要做的是将“单词”的每个单词(但只是一次)添加到“最终”,并将“数字”(这个单词出现在“单词”上的次数)添加到“时间”。有什么不对吗? 因为我得到OutOfMemoryError:Java堆空间

4 个答案:

答案 0 :(得分:1)

您不需要两个数组来查找单词及其计数。你可以在使用hashmap后得到这个细节。这个hashmap包含键,因为你的单词和值将是它的计数。 像一个hashmap

Map<String, Integer> words = new HashMap<String, Integer>();

然后您可以按照以下方式使用此地图

        try {
        //getting content from file.
        Scanner inputFile = new Scanner(new File("d:\\test.txt"));
                    //reading line by line
        while (inputFile.hasNextLine()) {
            // SringTokenize is automatically divide the string with space.

            StringTokenizer tokenizer = new StringTokenizer(
                    inputFile.nextLine());
            while (tokenizer.hasMoreTokens()) {
                String word = tokenizer.nextToken();
                // If the HashMap already contains the key, increment the
                // value
                if (words.containsKey(word)) {
                    words.put(word, words.get(word) + 1);
                }
                // Otherwise, set the value to 1
                else {
                    words.put(word, 1);
                }
            }

        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }// Loop through the HashMap and print the results
    for (Entry<String, Integer> entry : words.entrySet()) {
        String key = entry.getKey();
        Integer value = entry.getValue();

        System.out.println("Word"+key + ": its occurance " + value);

答案 1 :(得分:1)

我还认为使用Hashmap是最好的解决方案。

在您的代码中,有一个错误,也许您的问题就在这里。 替换以下内容:

        if(words.get(i)==Final.get(j)){

通过:

        if(words.get(i).equals(Final.get(j))){

答案 2 :(得分:0)

如果你要用尽内存,它会尝试将所有单词读入集合中。我建议你不要这样做,而是在你得到它们的时候统计这些词。

e.g。

Map<String, Integer> freq = new HashMap<>();
try(BufferedReader br = new BufferedReader(new FileReader(filename))) {
    for(String line; (line = br.readLine()) != null; ) {
         for(String word : line.trim().split("\\s+")) {
              Integer count = freq.get(word);
              freq.put(word, count == null ? 1 : 1 + count);
         }
    }
}

答案 3 :(得分:0)

试试这个例子。

String[] words = {"asdf","zvcxc", "asdf", "zxc","zxc", "zxc"};
        Map<String, Integer> result = new HashMap<String, Integer>();
        for (String word : words) {
            if (!result.containsKey(word)) {
                result.put(word, 1);
            } else {
                result.put(word, result.get(word) + 1);
            }
        }
        //print result
        for (Map.Entry<String, Integer> entry : result.entrySet()) {
            System.out.println(String.format("%s -- %s times", entry.getKey(), entry.getValue()));
        }

输出:

zvcxc - 1次

zxc - 3次

asdf - 2次