告诉我错误,为什么代码不起作用代码遍历数组并找到相同单词的总数,哪些是最多的
public static void main(String[] args) {
String [] Arr = {"abc", "ab", "abcd", "abc", "bla", "blabla", "blablabla", "bla.", "bla,", "bla!"};
String n = mostFrequent(Arr);
System.out.println(n);
}
public static String mostFrequent(String [] Arr) {
String oftGet = "";
//String [] Arr = {"bla", "bla", "net"};
int counter = 0;
int counter2 = 0;
for(int i = 0; i < Arr.length; i++) {
for (int j = 0; j < Arr.length; j++) {
if (Arr[i].equals(Arr[j])) {
counter++;
}
}
if(counter2 < counter) {
counter2 = counter;
oftGet = Arr[i];
counter = 0;
}
}
return oftGet;
}
1 个回答