【问题标题】:Can I get the count of string array, where it equals to a word?我可以得到字符串数组的计数,它等于一个单词吗?
【发布时间】:2017-07-30 20:23:03
【问题描述】:
 String[] sprüche = getResources().getStringArray(R.array.wörter);
 String[] textl=text.split(" ");       
 for (int kl=0;kl<textl.length;kl++){
            if (Arrays.asList(sprüche).contains(textl[kl])){


            }
        }

这是我的代码。我想知道“sprüche”在哪一点包含“textl [kl]”。 我必须做什么? 感谢您的帮助

【问题讨论】:

  • 提示:List.indexOf 几乎肯定是您在这里寻找的。请注意,您只需调用一次Arrays.asList - 您可以在整个循环中使用该列表...
  • 你必须解释为什么你这样做有困难,至少如果你想在这里得到答案的话。

标签: java android arrays string


【解决方案1】:

嗯,你说我可以在你的问题标题中得到计数吗?在帖子中你说单词出现在数组中的哪个点......嗯

试试这些代码:

public int sprücheContains(String []sprüche, String Textl )
{
    int k = sprüche.lenght();
    int count = 0;
    for (int k1 = 0; k1 < k; k1++){

         if (sprüche[k1].equals(textl) {
             count++;
         }
    }
    return count;   

}

它将返回textlsprüche中出现的次数


public List<Integer> sprücheContains(String []sprüche, String Textl )
{
    int k = sprüche.lenght();

    List<Integer> positions = new ArrayList<>();

    for (int k1 = 0; k1 < k; k1++){

         if (sprüche[k1].equals(textl) {
             positions.add(k1)
         }
    }
    return positions;   

}

这将返回Textlsprüche中出现的所有索引位置

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    相关资源
    最近更新 更多