Java 如何在不使用映射的情况下计算字符串在数组中出现的次数?

Java 如何在不使用映射的情况下计算字符串在数组中出现的次数?,java,collections,counting,Java,Collections,Counting,我试图计算字符串在数组中出现的次数,但我不能使用任何类型的映射。根据我的研究,我尝试了以下方法,希望它能奏效: int count = Collections.frequency(strings, search); 但是,当我在运行程序时出错时。该错误是一个空指针异常错误,我不知道如何修复该错误,也不知道为什么会发生。我的代码有什么问题吗 编辑:这是我的全部代码。我找不到问题所在。也许其他人能找到它 导入java.util.* 公共类Lab9{ public static void main

我试图计算字符串在数组中出现的次数,但我不能使用任何类型的映射。根据我的研究,我尝试了以下方法,希望它能奏效:

int count = Collections.frequency(strings, search);
但是,当我在运行程序时出错时。该错误是一个空指针异常错误,我不知道如何修复该错误,也不知道为什么会发生。我的代码有什么问题吗

编辑:这是我的全部代码。我找不到问题所在。也许其他人能找到它 导入java.util.*

公共类Lab9{

 public static void main(String[] args){
     Scanner kb =  new Scanner (System.in);
     String name = null;
     List<String> strings =null;
     System.out.println("How many strings do you want to enter?:");
     int size = kb.nextInt();

     List<String> String = getStrings(size,name);
     System.out.println(String);
     System.out.println("What string would you like to search for?:");
     String search = kb.next();

     int numofTimes = countValues (search,strings);
     System.out.print("That word appears "+numofTimes+" times in the array.");


 }
 public static List<String> getStrings(int size,String name) {
     Scanner kb = new Scanner (System.in);
     int count = 0;
     List<String> strings = new ArrayList<String>();
     while (count != size) {
         System.out.println("Enter a string:");
         name = kb.nextLine();
         strings.add(name);
         count = count + 1;
          }

return strings;

 }
 public static int countValues (String search, List<String> strings){

     int count = Collections.frequency(strings , search);



     return count;
 }



 }
publicstaticvoidmain(字符串[]args){
扫描仪kb=新扫描仪(System.in);
字符串名称=null;
列表字符串=null;
System.out.println(“要输入多少字符串?:”;
int size=kb.nextInt();
列表字符串=GetString(大小、名称);
System.out.println(字符串);
System.out.println(“您想要搜索什么字符串?:”;
字符串搜索=kb.next();
int numofTimes=countValues(搜索,字符串);
System.out.print(“该单词在数组中出现“+numofTimes+”次”);
}
公共静态列表GetString(整型大小,字符串名称){
扫描仪kb=新扫描仪(System.in);
整数计数=0;
列表字符串=新的ArrayList();
while(计数!=大小){
System.out.println(“输入字符串:”);
name=kb.nextLine();
字符串。添加(名称);
计数=计数+1;
}
返回字符串;
}
公共静态int countValues(字符串搜索、列表字符串){
int count=Collections.frequency(字符串、搜索);
返回计数;
}
}

在使用
字符串之前,对其进行空值和大小检查:

if (strings != null && strings.size() > 0) {
    int count = Collections.frequency(strings, search);
}
如果
size()

有一个使用频率的好例子(集合c,对象o)


如果集合中的某个值为null,则可能发生nullpointerexception。你确定这是你的频率线吗

您可以通过数组进行线性搜索

    String strings[] = {"A","B",null,"C","A",null}; // the array contains nulls
    String search = "A";
    int count = 0;
    for (int i = 0;i< strings.length ;i++ ) 
    {
        if(strings[i] != null)
        {
            if(strings[i].equals(search))
            {
                count++;
            }
        }                   
    }
    System.out.println(count);  
String strings[]={“A”,“B”,null,“C”,“A”,null};//数组包含空值
字符串搜索=“A”;
整数计数=0;
for(int i=0;i
这是以下代码:

公共静态整数频率(集合c,对象o){
int结果=0;
如果(o==null){
用于(对象e:c)
如果(e==null)
结果++;
}否则{
用于(对象e:c)
如果(o等于(e))
结果++;
}
返回结果;
}
如您所见,它能够处理
o==null
和包含
null
元素的
c
。它将抛出
NullPointerException
的唯一情况是当
c==null

此外,从文件中:

频率

公共静态整数频率(集合c,对象o)

抛出:

  • NullPointerException-如果c为null

NullPointerException——如果c为null,则会引发该异常。
(本例中的c为
字符串)
)数组用作参数,并在另一个方法中创建。为什么它是空的?这里有更多的代码:
code
publicstaticintcountvalues(stringsearch,List String){int count=Collections.frequency(String,search);returncount;}
code
请不要通过注释发布代码,编辑您的问题:)更清晰易读
public static int frequency(Collection<?> c, Object o) {
    int result = 0;
    if (o == null) {
        for (Object e : c)
            if (e == null)
                result++;
    } else {
        for (Object e : c)
            if (o.equals(e))
                result++;
    }
    return result;
}