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

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived