Java循环与增量问题

Java循环与增量问题,java,data-structures,loops,Java,Data Structures,Loops,有人能告诉我我的程序有什么问题吗 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; }

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

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;
        }
    }
}
字符串a[],b[];
int c[]=新int[b.长度];
对于(int j=0;j
我有数千个单词存储在
HashMap
中。现在我想在每个文件中检查一个单词在
allWords
中出现的次数


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

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

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]++;
        }
    }
}

然后,您可以使用
newData[j][k]

访问它。您可以在读取文件时计算字数,并将其存储在地图上。。 假设文件中的最后一个字是“-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());
    }
}
}
import java.util.HashMap;
导入java.util.Iterator;
导入java.util.Map;
导入java.util.Scanner;
公共类堆栈溢出{
@抑制警告(“未选中”)
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
Map countedWords=new HashMap();
int numberOfWords=0;
字串=”;
while(true){
word=scanner.nextLine();
if(单词equalsIgnoreCase(“-1”)){
打破
}
if(countedWords.containsKey(word)){
numberOfWords=countedWords.get(word);
countedWords.put(word,++numberOfWords);
}否则{
可数字。放(字,1);
}
}
迭代器it=countedWords.entrySet().Iterator();
while(it.hasNext()){
Map.Entry pairs=(Map.Entry)it.next();
System.out.println(pairs.getKey()+“=”+pairs.getValue());
}
}
}

这对性能不是很坏吗?我以前用过二维数组,但我不能增加它,然后我用另一个循环求和,结果它杀死了我的程序speed@user552961:二维数组本质上没有什么慢的。我想是你使用它的方式让它变慢了。我不知道你为什么认为你需要一个额外的循环-只需将
newdata[j]
更改为
newdata[j,k]
@Mark Byers:newdata[j,k]!?我不知道用Java访问二维数组的这种方式(这里是Java5)。您的意思是newData[]]和newint[oneFileWords.length][allWords.length],您的意思是使用newData[j][k]而不是newData[j,k]来访问它?我不确定,所以我不敢亲自编辑你的答案。@Sponbender:对不起,你可能是对的-我没有测试它。@Mark Byers,当你打印二维数组时,它不会给你与我在问题中要求的相同的输出。它只会产生,例如,0 1 0 0 0 1 0\n 0 0 0 1 0 1\n 1 0 1 0 1 0 0 0,然后我必须使用另一个循环对所有这些值求和,以获得1个文件计算字。在当前的实现中,您遇到了哪些错误?到目前为止,我看到的一个可能的错误是,您从未初始化
oneFileWords
allWords
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());
    }
}
}