Java循环和增量问题

发布于 2024-10-08 18:59:33 字数 413 浏览 0 评论 0原文

谁能告诉我我的程序有什么问题吗?

String a[],b[];
int c[] = new int[b.length];

for (int j = 0; j < a.length; j++) {
    for (int k = 0; k < b.length; k++) {
        if (b[k].equals(a[j])) {
            c[k]++;
        } else {
            c[k] = 0;
        }
    }
}

我在 HashMap 中存储了数千个单词。现在我想检查每个文件中 allWords 中某个单词出现的次数。

你能指出我的程序中的错误或者给我你的想法我该怎么做吗?

Can any one tell me what is the problem in my program?

String a[],b[];
int c[] = new int[b.length];

for (int j = 0; j < a.length; j++) {
    for (int k = 0; k < b.length; k++) {
        if (b[k].equals(a[j])) {
            c[k]++;
        } else {
            c[k] = 0;
        }
    }
}

I have thousands of words stored in a HashMap. Now I want to check in every file that how many time one word occurred from allWords.

Can you point out mistake in my program or give me your idea that how I can do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九歌凝 2024-10-15 18:59:34

您可以在阅读文件时计算单词数并存储在地图上。
假设文件中的最后一个单词是“-1”,并且一行中只有一个单词,即使该单词是“生日快乐”,我也会这样做:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class StackOverflow {

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    Map<String, Integer> countedWords = new HashMap<String, Integer>();
    int numberOfWords = 0;
    String word = "";
    while (true) {
        word = scanner.nextLine();
        if (word.equalsIgnoreCase("-1")) {
            break;
        }
        if (countedWords.containsKey(word)) {
            numberOfWords = countedWords.get(word);
            countedWords.put(word, ++numberOfWords);
        } else {
            countedWords.put(word, 1);
        }
    }
    Iterator it = countedWords.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
    }
}
}

You could count words while reading your files and store on a Map already..
Assuming last word on file is "-1" and there's only one word in a line, even if the word is "happy birthday", I'd do something like this:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class StackOverflow {

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    Map<String, Integer> countedWords = new HashMap<String, Integer>();
    int numberOfWords = 0;
    String word = "";
    while (true) {
        word = scanner.nextLine();
        if (word.equalsIgnoreCase("-1")) {
            break;
        }
        if (countedWords.containsKey(word)) {
            numberOfWords = countedWords.get(word);
            countedWords.put(word, ++numberOfWords);
        } else {
            countedWords.put(word, 1);
        }
    }
    Iterator it = countedWords.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
    }
}
}
微凉徒眸意 2024-10-15 18:59:33

我认为这一行不必要地重置您的计数器:

newData[j] = 0;

尝试删除它:

for (int j = 0; j < oneFileWords.length; j++) {
    for (int k = 0; k < allWords.length; k++) {
        if (allWords[k].equals(oneFileWords[j])) {
            newData[j]++;
        }
    }
}

如果您想为每个文件中的每个单词保留单独的计数,那么您将需要使用二维数组。

int newData[][] = new int[oneFileWords.length][allWords.length];

然后您可以使用 newData[j][k] 访问它。

I think this line is resetting your counters unnecessarily:

newData[j] = 0;

Try removing it:

for (int j = 0; j < oneFileWords.length; j++) {
    for (int k = 0; k < allWords.length; k++) {
        if (allWords[k].equals(oneFileWords[j])) {
            newData[j]++;
        }
    }
}

If you want to keep a separate count for each word in each file then you will need to use two dimensional array.

int newData[][] = new int[oneFileWords.length][allWords.length];

You can then access it using newData[j][k].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文