Java 检索包含值的所有项的索引

Java 检索包含值的所有项的索引,java,Java,我需要获得所有索引号,我将获得关键字'Articles'的匹配项,并且我希望计数器'indexoccurrencecounter'仅在我获得匹配项时递增 List<String> valueslist = new ArrayList<String>(); valueslist.add("Articles"); valueslist.add("Vals"); valueslist.add("Articles"); valueslist.add("Toast"); Stri

我需要获得所有索引号,我将获得关键字'Articles'的匹配项,并且我希望计数器'indexoccurrencecounter'仅在我获得匹配项时递增

List<String> valueslist = new ArrayList<String>();
valueslist.add("Articles");
valueslist.add("Vals");
valueslist.add("Articles");
valueslist.add("Toast");

String key="Articles";

System.out.println("List contents having values are: "+valueslist);
int ind=0;
int indexoccurencecounter=0;
for (int i=0;i<valueslist.size();i++){
    ind=valueslist.indexOf(key);    
    if (ind>=0){
        indexoccurencecounter++;
    }   
}
System.out.println("Index's of the key "+key+" is: "+ind);
System.out.println("The key specified appears "+indexoccurencecounter+" times in the result links");

由于多个索引将匹配,
intind
无法跟踪所有索引。它只能跟踪一个。我建议您创建一个索引的
列表。这样做的一个有用的副作用是,您不再需要计算发生次数,只需使用列表的
size()
方法即可

List values=new ArrayList();
价值。添加(“条款”);
价值。添加(“VAL”);
价值。添加(“条款”);
价值。添加(“Toast”);
字符串searchTerm=“Articles”;
List matchingIndices=新的ArrayList();
对于(int i=0;i=0){
匹配骰子。添加(i);
}
}
int numberOfMatches=matchingIndices.size();
System.out.println(“值:”+值);
System.out.println(“键的索引”“+searchTerm+”:“+MatchingDices”);
System.out.println(“键出现”+numberOfMatches+“times”);
产生:

值:[文章、VAL、文章、吐司]
关键“文章”索引:[0,2]
该键出现2次。

由于多个索引将匹配,
int ind
无法跟踪所有索引。它只能跟踪一个。我建议您创建一个索引的
列表。这样做的一个有用的副作用是,您不再需要计算发生次数,只需使用列表的
size()
方法即可

List values=new ArrayList();
价值。添加(“条款”);
价值。添加(“VAL”);
价值。添加(“条款”);
价值。添加(“Toast”);
字符串searchTerm=“Articles”;
List matchingIndices=新的ArrayList();
对于(int i=0;i=0){
匹配骰子。添加(i);
}
}
int numberOfMatches=matchingIndices.size();
System.out.println(“值:”+值);
System.out.println(“键的索引”“+searchTerm+”:“+MatchingDices”);
System.out.println(“键出现”+numberOfMatches+“times”);
产生:

值:[文章、VAL、文章、吐司]
关键“文章”索引:[0,2]
该键出现2次。
List contents having values are: [Articles, Vals, Articles, Toast]
Index's of the key Articles is: 0,2
The key specified appears 2 times in the result links