Java Arraylist remove multiple element by index

后端 未结 8 1565
后悔当初
后悔当初 2020-12-19 06:54

Here is my code:

for (int i = 0; i < myarraylist.size(); i++) {
        for (int j = 0; j < stopwords.size(); j++) {
            if (stopwords.get(j).e         


        
8条回答
  •  伪装坚强ぢ
    2020-12-19 07:11

    Ok this is very good problem to solve. Lets start with an exmaple

    We have data = [5,2,7,3,9,34,63,23,85,23,94,7]

    indexes to remove

    int index[] = [5,2,7,9]
    

    Note : As you remove single item from array other elements get shifted by 1.

    If we use ArrayList to remove elements of indexes then first you need to sort the indexes in descending.

    i.e indexes = [9,7,5,2] then remove the element from index

    ArrayList data = Arrays.asList(new Integer[] {5,2,7,3,9,34,63,23,85,23,94,7});
    
    for(int i=0;i

提交回复
热议问题