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?
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.
java
marked as duplicate by Tom, Andreas, Stephen C
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.
|
show 9 more comments
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.
java
marked as duplicate by Tom, Andreas, Stephen C
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 byArrays.asList( int[] )
.
– Roddy of the Frozen Peas
Mar 8 at 0:01
1
Also, don't use==
forInteger
, useequals()
.
– 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
|
show 9 more comments
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.
java
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
java
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
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
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 byArrays.asList( int[] )
.
– Roddy of the Frozen Peas
Mar 8 at 0:01
1
Also, don't use==
forInteger
, useequals()
.
– 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
|
show 9 more comments
1
Likely your List is some form of unmodifiable list, such as one created byArrays.asList( int[] )
.
– Roddy of the Frozen Peas
Mar 8 at 0:01
1
Also, don't use==
forInteger
, useequals()
.
– 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
|
show 9 more comments
1 Answer
1
active
oldest
votes
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.
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 useIterator::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)
wheni == 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 andget(index)
,set(index)
,remove(index)
etcetera. Basically, what the OP's example does!
– Stephen C
Mar 8 at 0:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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 useIterator::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)
wheni == 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 andget(index)
,set(index)
,remove(index)
etcetera. Basically, what the OP's example does!
– Stephen C
Mar 8 at 0:35
add a comment |
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.
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 useIterator::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)
wheni == 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 andget(index)
,set(index)
,remove(index)
etcetera. Basically, what the OP's example does!
– Stephen C
Mar 8 at 0:35
add a comment |
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.
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.
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 useIterator::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)
wheni == 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 andget(index)
,set(index)
,remove(index)
etcetera. Basically, what the OP's example does!
– Stephen C
Mar 8 at 0:35
add a comment |
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 useIterator::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)
wheni == 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 andget(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
add a comment |
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
==
forInteger
, useequals()
.– 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