1

I am trying to remove duplicates from an ArrayList. But I keep getting this UnsupportedOperationException

public static void removeDuplicates(List<Integer> list) {
    Collections.sort(list);

    for(int i = 0; i<list.size();i++) {
        if(list.get(i)== list.get((i+1))) {
            list.remove(i+1);
        }

    }

} 

One thing I cannot create a new list and change it because I shouldn't return anything. I have to change the list in place.

  • 1
    Likely your List is some form of unmodifiable list, such as one created by `Arrays.asList( int[] )`. – Roddy of the Frozen Peas Mar 08 '19 at 00:01
  • I'd like to bet, then you're not using an ArrayList, but either `Arrays.asList(...)` or any other unmodifiable list. – Tom Mar 08 '19 at 00:01
  • Of course, even if you used a modifiable list, [then you'd get a ConcurrentModificationException](https://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re) when you called `list.remove` in that loop. – Roddy of the Frozen Peas Mar 08 '19 at 00:02
  • A list created by `Arrays.asList( int[] )` wouldn't be eligible to be passed to `removeDuplicates`. – Tom Mar 08 '19 at 00:02
  • 1
    Also, don't use `==` for `Integer`, use `equals()`. – RaminS Mar 08 '19 at 00:03
  • 1
    @RoddyoftheFrozenPeas No, there wouldn't be a ConcurrentModificationException. – Tom Mar 08 '19 at 00:03
  • Yea its Array.asList(int[ ]). How can i deal with it? – Yojan Gautam Mar 08 '19 at 00:11
  • No, it can't be `Array.asList(int[ ])`, it is something else, like `Array.asList(1,2,3)`. Using an int array would cause different problems. – Tom Mar 08 '19 at 00:17
  • Its List list1 = Arrays.asList(1,2,3,4,4,3,2,1); that is being passed – Yojan Gautam Mar 08 '19 at 00:19
  • Your question is a duplicate of [remove() on List created by Arrays.asList() throws UnsupportedOperationException](//stackoverflow.com/q/7885573) – Tom Mar 08 '19 at 00:19
  • I cannot create a new because I have to change the value in the given List. I shouldn't return anything. – Yojan Gautam Mar 08 '19 at 00:23
  • 1
    Well you have to change your design. You fundamentally **cannot** remove an element from an array. The Java language spec and the JVM spec do not allow this. (And if you can't change the design, or figure out an *alternative* way that doesn't involve changing the array's length, you need to consider abandoning the project entirely. This is a bit like saying, "I need to modify maths so that 1 + 1 is 3 for my project". It won't work.) – Stephen C Mar 08 '19 at 00:26
  • That means my professor is wrong. He wants me to remove it. But I can't figure out how. – Yojan Gautam Mar 08 '19 at 00:30
  • 1
    I think it is more likely that you have not *understood* what your professor is *really* saying and/or asking you to do. Go talk to him ... and be prepared to "eat humble pie". – Stephen C Mar 08 '19 at 00:31

1 Answers1

-1

You cannot remove elements whilst iterating over it using a for loop. A simple way around this is to create a new ArrayList containing your original lists elements and then loop over the original list removing duplicates from your copy of the list and then return that.

Even if you could remove elements using a for loop the other thing this could be is if the list of type UnmodifiableCollection? If so you cannot remove elements from it, which is the point of the class. Again a solution would be to create a brand new ArrayList which copies the original elements and remove and alter that however you like and return that to the calling code.

tomgeraghty3
  • 1,234
  • 6
  • 10
  • Actually, in some cases you can remove elements while iterating a list. If you are indexing the list, it is OK. And if you are iterating with an iterator (explicitly) you can use `Iterator::remove`. The OP's could *would* work provided that this was a list that supported remove ... and he corrected the bugs in the indexing; e.g. `get(i+1)` when `i == list.length() - 1`. – Stephen C Mar 08 '19 at 00:13
  • I agree about the remove() of Iterator. Can you explain what you mean by 'if you are indexing the list' – tomgeraghty3 Mar 08 '19 at 00:15
  • Using using a loop to iterate over the indexes of an array and `get(index)`, `set(index)`, `remove(index)` etcetera. Basically, what the OP's example does! – Stephen C Mar 08 '19 at 00:35