Java循环和增量问题
谁能告诉我我的程序有什么问题吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在阅读文件时计算单词数并存储在地图上。
假设文件中的最后一个单词是“-1”,并且一行中只有一个单词,即使该单词是“生日快乐”,我也会这样做:
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:
我认为这一行不必要地重置您的计数器:
尝试删除它:
如果您想为每个文件中的每个单词保留单独的计数,那么您将需要使用二维数组。
然后您可以使用
newData[j][k]
访问它。I think this line is resetting your counters unnecessarily:
Try removing it:
If you want to keep a separate count for each word in each file then you will need to use two dimensional array.
You can then access it using
newData[j][k]
.