Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-如何比较从拆分字符串创建的列表中的元素_Java_String_List_Split - Fatal编程技术网

Java-如何比较从拆分字符串创建的列表中的元素

Java-如何比较从拆分字符串创建的列表中的元素,java,string,list,split,Java,String,List,Split,我需要看看在一个很长的字符串中是否有任何单词是“the”(然后计算发生这种情况的次数) 因此,我将字符串拆分为列表中的各个元素 String [ ] list1 = phrase1.split(" "); 如何将列表中的各个元素与字符串“the”进行比较 我会使用一个数组 int count = 0; for (int i = 0; i < string.length; i++) { if (string[i].equals("the")) { count++;

我需要看看在一个很长的字符串中是否有任何单词是“the”(然后计算发生这种情况的次数) 因此,我将字符串拆分为列表中的各个元素

String [ ] list1 = phrase1.split(" "); 
如何将列表中的各个元素与字符串“the”进行比较

我会使用一个数组

int count = 0;
for (int i = 0; i < string.length; i++)
 {
   if (string[i].equals("the"))
   {
     count++;
   }
}

return count;

它不会编译。我做错了什么

实际上,您可以在列表中使用
equals
,但不是您想要的方式。此外,您还有一个
字符串[]
,一个字符串数组,而不是
列表(字符串列表)

您要做的是迭代该数组,并将每个项(一个单词)与“the”进行比较:


可以对数组和列表使用修改后的for循环

String[] list1 = phrase1.split(" "); 

int count = 0;
for (String item : list1)
 {
   if (item.equals("the"))
   {
     count++;
   }
}

return count;

您正在将不正确的变量“string[i]”与“the”进行比较。将列表1重命名为字符串或将以下两条语句更改为:

for (int i = 0; i < list1.length; i++)

if (list1[i].equals("the"))
for(int i=0;i
这是对您的编辑的回答

代码将无法成功编译,因为

String[] list1 = list.split(" "); //here list is String[]   
围绕给定正则表达式的匹配项拆分此字符串。不能调用字符串[]的数组

您有
String[]列表
,希望检查给定字符串数组中的字符串“the”,然后只需使用

检查对象是否在给定数组中

如果传入空数组,则该方法返回false

参数:
数组-要搜索的数组
objectToFind—要查找的对象

返回:
如果数组包含对象,则为true

使用需要jar下载的
ArrayUtils
类。你可以从

您还需要计算字符串“the”在给定字符串中的出现次数,然后可以使用

计算子字符串在较大字符串中出现的次数

null或空(“”)字符串输入返回0

StringUtils.countMatches(null, *)       = 0
StringUtils.countMatches("", *)         = 0
StringUtils.countMatches("abba", null)  = 0
StringUtils.countMatches("abba", "")    = 0
StringUtils.countMatches("abba", "a")   = 2
StringUtils.countMatches("abba", "ab")  = 1
StringUtils.countMatches("abba", "xxx") = 0
参数:
str-要检查的字符串,可能为空
sub-要计数的子字符串可能为空

返回:
出现的次数,如果任一字符串为空,则为0


单击以获取StringUtils类的jar有很多方法可以实现它

  • 使用下面的代码

    String str = "abc the def the ghi the";
    String find = "the";
    
    int i = 0;
    int count = 0;
    while(str.indexOf(find, i) != -1) {
        count++;
        i = str.indexOf(find, i) + find.length();
    }
    System.out.println(count);
    
  • 如果您想使用ApacheCommonsLang库[org.apache.commons.lang.StringUtils],那么您可以执行以下操作

    String str = "abc the def the ghi the";
    String find = "the";
    int count = StringUtils.countMatches(str, find)
    System.out.println(count);
    

因此,这里的问题是将列表中名为list1的每个项目与代码中的字符串“the”进行比较。将“string”替换为“list1”,但是
list1
是一个数组。这里有什么问题?是的,我需要将列表中的每个项目与回答编辑的字符串“the”进行比较-您需要更改该方法签名-删除
[]
之前的
列表
public static int countMatches(String str,
                           String sub)
StringUtils.countMatches(null, *)       = 0
StringUtils.countMatches("", *)         = 0
StringUtils.countMatches("abba", null)  = 0
StringUtils.countMatches("abba", "")    = 0
StringUtils.countMatches("abba", "a")   = 2
StringUtils.countMatches("abba", "ab")  = 1
StringUtils.countMatches("abba", "xxx") = 0
String str = "abc the def the ghi the";
String find = "the";

int i = 0;
int count = 0;
while(str.indexOf(find, i) != -1) {
    count++;
    i = str.indexOf(find, i) + find.length();
}
System.out.println(count);
String str = "abc the def the ghi the";
String find = "the";
int count = StringUtils.countMatches(str, find)
System.out.println(count);