Home > Software engineering >  Remove values in ArrayList at indexes specified in int[]
Remove values in ArrayList at indexes specified in int[]

Time:01-06

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());
    

}

CodePudding user response:

There are 2 remove methods available for Lists, 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.

CodePudding user response:

    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 Integers in the list, only Strings (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.

CodePudding user response:

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;
 }

CodePudding user response:

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));
}
  •  Tags:  
  • Related