我有一个ArrayList,其中包含不同索引处的重复值。
例如 {"Indian","American","Chinese","Australian","Indian","Russian","Indian"}
如您所见,值 - "Indian"
存在于索引 - 0
,4
和 6
。
我需要知道所有这些索引,其中存在 "Indian"
并创建该数组列表。
以下是我的代码:
public void filter(){
categoryArray = Arrays.asList(category);
for(String k : category){
//Log.v("filter", filterTerm);
if(k.equals(filterTerm.toLowerCase()))
{
int p = categoryArray.indexOf(k);
Log.v("index of categArr", ""+p);
String id = Integer.toString(p);
indexes.add(id);
}// end of if
}// end of for
在这里,我通过获取索引(ArrayList)的大小来得知重复出现的次数,但是当我检查值时,所有的索引都只有一个值,因为在方法
indexOf()
中,它总是返回在数组中找到的第一个值的索引。所以如果重复存在于索引-
2
,5
,7
,我得到的索引数组大小为3
。但实际上的值是{2,2,2,}
。