UnsupportedOperationException While removing [duplicate]remove() on List created by Arrays.asList() throws UnsupportedOperationExceptionIterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loopIterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loopWhy do I get an UnsupportedOperationException when trying to remove an element from a List?IntelliJ inspection gives “Cannot resolve symbol” but still compiles codeAndroid-java- How to sort a list of objects by a certain value within the objectHow to get another Calendar result from same instance?Why does List.add(E) return boolean while List.Add(int, E) returns void?Remove entries from Java LinkedHashMaphow to sort a list according to a specific field in one line? (in java)Adding a string to a static ArrayList in another class resulted in UnsupportedOperationExceptionHow to implement parcelable with my custom class containing Hashmap and SparseArray?

How many arrows is an archer expected to fire by the end of the Tyranny of Dragons pair of adventures?

Does the reader need to like the PoV character?

The Digit Triangles

Why should universal income be universal?

US tourist/student visa

Why do Radio Buttons not fill the entire outer circle?

Is there a way to have vectors outlined in a Vector Plot?

What fields between the rationals and the reals allow a good notion of 2D distance?

Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?

Has the laser at Magurele, Romania reached a tenth of the Sun's power?

Did the UK lift the requirement for registering SIM cards?

Taxes on Dividends in a Roth IRA

How to draw a matrix with arrows in limited space

The IT department bottlenecks progress, how should I handle this?

How to make money from a browser who sees 5 seconds into the future of any web page?

What to do when eye contact makes your coworker uncomfortable?

Why Shazam when there is already Superman?

When were female captains banned from Starfleet?

Giving feedback to someone without sounding prejudiced

Do we have to expect a queue for the shuttle from Watford Junction to Harry Potter Studio?

Is this part of the description of the Archfey warlock's Misty Escape feature redundant?

Is there any evidence that Cleopatra and Caesarion considered fleeing to India to escape the Romans?

What is Cash Advance APR?

15% tax on $7.5k earnings. Is that right?



UnsupportedOperationException While removing [duplicate]


remove() on List created by Arrays.asList() throws UnsupportedOperationExceptionIterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loopIterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loopWhy do I get an UnsupportedOperationException when trying to remove an element from a List?IntelliJ inspection gives “Cannot resolve symbol” but still compiles codeAndroid-java- How to sort a list of objects by a certain value within the objectHow to get another Calendar result from same instance?Why does List.add(E) return boolean while List.Add(int, E) returns void?Remove entries from Java LinkedHashMaphow to sort a list according to a specific field in one line? (in java)Adding a string to a static ArrayList in another class resulted in UnsupportedOperationExceptionHow to implement parcelable with my custom class containing Hashmap and SparseArray?













1
















This question already has an answer here:



  • remove() on List created by Arrays.asList() throws UnsupportedOperationException

    3 answers



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.










share|improve this question















marked as duplicate by Tom, Andreas, Stephen C java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 0:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    Likely your List is some form of unmodifiable list, such as one created by Arrays.asList( int[] ).

    – Roddy of the Frozen Peas
    Mar 8 at 0:01






  • 1





    Also, don't use == for Integer, use equals().

    – Gendarme
    Mar 8 at 0:03







  • 1





    @RoddyoftheFrozenPeas No, there wouldn't be a ConcurrentModificationException.

    – Tom
    Mar 8 at 0:03






  • 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 8 at 0:26







  • 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 8 at 0:31
















1
















This question already has an answer here:



  • remove() on List created by Arrays.asList() throws UnsupportedOperationException

    3 answers



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.










share|improve this question















marked as duplicate by Tom, Andreas, Stephen C java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 0:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    Likely your List is some form of unmodifiable list, such as one created by Arrays.asList( int[] ).

    – Roddy of the Frozen Peas
    Mar 8 at 0:01






  • 1





    Also, don't use == for Integer, use equals().

    – Gendarme
    Mar 8 at 0:03







  • 1





    @RoddyoftheFrozenPeas No, there wouldn't be a ConcurrentModificationException.

    – Tom
    Mar 8 at 0:03






  • 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 8 at 0:26







  • 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 8 at 0:31














1












1








1









This question already has an answer here:



  • remove() on List created by Arrays.asList() throws UnsupportedOperationException

    3 answers



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.










share|improve this question

















This question already has an answer here:



  • remove() on List created by Arrays.asList() throws UnsupportedOperationException

    3 answers



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.





This question already has an answer here:



  • remove() on List created by Arrays.asList() throws UnsupportedOperationException

    3 answers







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 0:27







Yojan Gautam

















asked Mar 7 at 23:59









Yojan GautamYojan Gautam

64




64




marked as duplicate by Tom, Andreas, Stephen C java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 0:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Tom, Andreas, Stephen C java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 8 at 0:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1





    Likely your List is some form of unmodifiable list, such as one created by Arrays.asList( int[] ).

    – Roddy of the Frozen Peas
    Mar 8 at 0:01






  • 1





    Also, don't use == for Integer, use equals().

    – Gendarme
    Mar 8 at 0:03







  • 1





    @RoddyoftheFrozenPeas No, there wouldn't be a ConcurrentModificationException.

    – Tom
    Mar 8 at 0:03






  • 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 8 at 0:26







  • 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 8 at 0:31













  • 1





    Likely your List is some form of unmodifiable list, such as one created by Arrays.asList( int[] ).

    – Roddy of the Frozen Peas
    Mar 8 at 0:01






  • 1





    Also, don't use == for Integer, use equals().

    – Gendarme
    Mar 8 at 0:03







  • 1





    @RoddyoftheFrozenPeas No, there wouldn't be a ConcurrentModificationException.

    – Tom
    Mar 8 at 0:03






  • 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 8 at 0:26







  • 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 8 at 0:31








1




1





Likely your List is some form of unmodifiable list, such as one created by Arrays.asList( int[] ).

– Roddy of the Frozen Peas
Mar 8 at 0:01





Likely your List is some form of unmodifiable list, such as one created by Arrays.asList( int[] ).

– Roddy of the Frozen Peas
Mar 8 at 0:01




1




1





Also, don't use == for Integer, use equals().

– Gendarme
Mar 8 at 0:03






Also, don't use == for Integer, use equals().

– Gendarme
Mar 8 at 0:03





1




1





@RoddyoftheFrozenPeas No, there wouldn't be a ConcurrentModificationException.

– Tom
Mar 8 at 0:03





@RoddyoftheFrozenPeas No, there wouldn't be a ConcurrentModificationException.

– Tom
Mar 8 at 0:03




1




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 8 at 0:26






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 8 at 0:26





1




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 8 at 0:31






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 8 at 0:31













1 Answer
1






active

oldest

votes


















-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.






share|improve this answer























  • 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 8 at 0:13











  • I agree about the remove() of Iterator. Can you explain what you mean by 'if you are indexing the list'

    – tomgeraghty3
    Mar 8 at 0: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 8 at 0:35


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-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.






share|improve this answer























  • 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 8 at 0:13











  • I agree about the remove() of Iterator. Can you explain what you mean by 'if you are indexing the list'

    – tomgeraghty3
    Mar 8 at 0: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 8 at 0:35
















-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.






share|improve this answer























  • 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 8 at 0:13











  • I agree about the remove() of Iterator. Can you explain what you mean by 'if you are indexing the list'

    – tomgeraghty3
    Mar 8 at 0: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 8 at 0:35














-1












-1








-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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 0:03









tomgeraghty3tomgeraghty3

43128




43128












  • 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 8 at 0:13











  • I agree about the remove() of Iterator. Can you explain what you mean by 'if you are indexing the list'

    – tomgeraghty3
    Mar 8 at 0: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 8 at 0:35


















  • 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 8 at 0:13











  • I agree about the remove() of Iterator. Can you explain what you mean by 'if you are indexing the list'

    – tomgeraghty3
    Mar 8 at 0: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 8 at 0:35

















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 8 at 0:13





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 8 at 0:13













I agree about the remove() of Iterator. Can you explain what you mean by 'if you are indexing the list'

– tomgeraghty3
Mar 8 at 0:15





I agree about the remove() of Iterator. Can you explain what you mean by 'if you are indexing the list'

– tomgeraghty3
Mar 8 at 0: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 8 at 0:35






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 8 at 0:35






Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page