查找ArrayList中重复值的索引

Ank*_*kit 6 java arraylist

我有一个ArrayList,它包含diff diff index的重复值.例如{"Indian","American","Chinese","Australian","Indian","Russian","Indian"} ,你可以看到值 - "Indian"存在于索引 - 0,4&6.

我需要知道所有这些索引"Indian"存在并创建一个arrayList.这是我的代码:

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
Run Code Online (Sandbox Code Playgroud)

在这里,我通过获取索引的大小(ArrayList)得到重复发生的次数,但是当我检查值时.它在所有索引中的一个值,因为在方法中:indexOf()它总是带来它在Array中找到的第一个值的索引.

所以,如果存在重复索引- ,,2 我得到的指数作为数组大小.但价值观是573{2,2,2,};

Rob*_*een 2

您需要知道当前位于数组中的哪个索引,而不是要找到它的第一个索引。为了跟踪这一点,请将

int i = 0;
Run Code Online (Sandbox Code Playgroud)

在循环之前和循环的最后放置

i++;
Run Code Online (Sandbox Code Playgroud)

然后该变量i会告诉您在哪里找到该值,以便您可以将其添加i到索引列表中。