Java计算ArrayList中字符串的出现次数
我编写了一个程序,其中有N个字符串和Q个查询,它们也是字符串。目标是确定每个查询在N个字符串中出现的次数。 这是我的代码:Java计算ArrayList中字符串的出现次数,java,string,arraylist,Java,String,Arraylist,我编写了一个程序,其中有N个字符串和Q个查询,它们也是字符串。目标是确定每个查询在N个字符串中出现的次数。 这是我的代码: import java.util.Scanner; import java.util.ArrayList; public class SparseArrays{ // count the number of occurances of a string in an array int countStringOccurance(ArrayList<String&g
import java.util.Scanner;
import java.util.ArrayList;
public class SparseArrays{
// count the number of occurances of a string in an array
int countStringOccurance(ArrayList<String> arr, String str){
int count = 0;
for (int i=0; i<arr.size(); i++) {
if (str==arr.get(i)) {
count += 1;
}
}
return count;
}
void start(){
// scanner object
Scanner input = new Scanner(System.in);
// ask for user to input num strings
System.out.println("How many string would you like to enter?");
// store the number of strings
int numStrings = input.nextInt();
// to get rid of extra space
input.nextLine();
// ask user to enter strings
System.out.println("Enter the "+numStrings+" strings.");
ArrayList<String> stringInputArray = new ArrayList<String>();
for (int i=0; i<numStrings; i++) {
stringInputArray.add(input.nextLine());
} // all strings are in the stringInputArray
// ask user to input num queries
System.out.println("Enter number of queries.");
int numQueries = input.nextInt();
// to get rid of extra space
input.nextLine();
// ask user to enter string queries
System.out.println("Enter the "+numQueries+" queries.");
ArrayList<String> stringQueriesArray = new ArrayList<String>();
for (int i=0; i<numQueries; i++) {
stringQueriesArray.add(input.nextLine());
} // all string queries are in the stringQueriesArray
for (int i=0; i<stringQueriesArray.size(); i++) {
int result =
countStringOccurance(stringInputArray,stringQueriesArray.get(i));
System.out.println(result);
}
}
void printArr(ArrayList<String> arr){
for (int i=0; i<arr.size(); i++) {
System.out.println(arr.get(i));
}
System.out.println(" ");
}
public static void main(String[] args) {
SparseArrays obj = new SparseArrays();
obj.start();
}
}
import java.util.Scanner;
导入java.util.ArrayList;
公共级麻雀{
//计算数组中字符串出现的次数
int countStringOccurance(ArrayList arr,String str){
整数计数=0;
对于(int i=0;i使用String.equals()
比较字符串,而不是=
。而不是:
if (str1==str2) // WRONG!!!
写
//计算数组中字符串的出现次数
int countStringOccurance(ArrayList arr,String str){
整数计数=0;
对于(int i=0;i在两个对象之间使用.equals()
,而不是=
。然后,当出现错误时,尝试使用simpleSystem.out.println()在敏感部分放置一些日志跟踪
例如,为了检查每个对象和变量是否已正确填充。可以帮助您解决此类问题!已经有一个内置的方法来处理此问题,集合#频率
:
int occurrences = Collections.frequency(list, "String here");
不要像这样比较字符串-->if(str==arr.get(i))
,应该是-->if(str.equals(arr.get(i))
@Aominé谢谢!这解决了问题。如果比较不需要区分大小写,还有.equalsIgnoreCase()
。
// count the number of occurances of a string in an array
int countStringOccurance(ArrayList<String> arr, String str){
int count = 0;
for (int i=0; i<arr.size(); i++) {
if (str.equals(arr.get(i))) { //<-- change your condition to this
count += 1;
}
}
return count;
}
int occurrences = Collections.frequency(list, "String here");