简体   繁体   中英

Remove values in ArrayList at indexes specified in int[]

The method takes an array of ints which are the indexes, and an ArrayList<String> . Items in the ArrayList with the index of the values in the index array are to be removed from the list.

When I run the following code, no items are removed. Can anyone explain what I'm doing wrong here?

  public static ArrayList<String> removeItems (ArrayList<String> list, int[]indexes){
         
         TreeSet<Integer> set = new TreeSet<>();
         
         for(Integer i : indexes) {
             set.add(i);
         }
         
        for(Integer i : set) {
            list.remove(i);
            i--;
        }
         
         return list;
     }

main method:

public static void main(String[] args) {
        
        ArrayList<String>list = new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        
    int[] arr = {2,3};

    ArrayList<String> newList = removeItems(list, arr);
    
    System.out.println(newList.toString());
    

}

There are 2 remove methods available for List s, which seem somewhat identical, but do very different things:

As you can see the 2nd method takes in an Object . Integer is an instance of Object . int on the other hand, is a primitive type. To be able to solve your problem you need to make sure you pass int to remove() instead of Integer .

    for(Integer i : set) {
        list.remove(i);
        i--;
    }

is attempting to remove the object i from the list . i is an Integer , and there aren't any Integer s in the list, only String s (assuming no heap pollution).

If you mean to remove things at the index : i :

    for(int i : set) {
        list.remove(i);
        i--;
    }

Note that the i-- is redundant; and this won't remove the thing you intend after the first removal. You should iterate the set in reverse order to avoid this issue.

please take a look at the article in stackoverflow, then you can see the answer to your problem. Remove element of a regular array [1]:

Solved:

 public static ArrayList<String> removeItems (ArrayList<String> list, int[]indexes){
     
     TreeSet<Integer> set = new TreeSet<>();
     
     for(Integer i : indexes) {
         set.add(i);
     }
     
    int num = 0;
    for(int i : set) {
        list.remove(i-num);
        num++;
        }       
        
     return list;
 }
 

Since your function is returning a list, I'd take a slightly different approach and take advantage of that by building a new one with just the indexes that aren't in the list of ones you want to remove:

public static ArrayList<String> removeItems(ArrayList<String> list,
                                            int[] indexes) {
    // Could also use a TreeSet or HashSet here. As long as it has better
    // than O(n) checking for a particular value.
    int[] sorted = Arrays.copyOf(indexes, indexes.length);
    Arrays.sort(sorted);
    return IntStream.range(0, list.size())
                    .filter(i -> Arrays.binarySearch(sorted, i) < 0)
                    .mapToObj(i -> list.get(i))
                    .collect(Collectors.toCollection(ArrayList::new));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM