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?
Etiquette around loan refinance - decision is going to cost first broker a lot of money
Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?
Is it possible to download Internet Explorer on my Mac running OS X El Capitan?
What to put in ESTA if staying in US for a few days before going on to Canada
Did Shadowfax go to Valinor?
Neighboring nodes in the network
Brothers & sisters
How badly should I try to prevent a user from XSSing themselves?
Western buddy movie with a supernatural twist where a woman turns into an eagle at the end
Can I make "comment-region" comment empty lines?
Is it legal for company to use my work email to pretend I still work there?
Can a rocket refuel on Mars from water?
How do I write bicross product symbols in latex?
Does casting Light, or a similar spell, have any effect when the caster is swallowed by a monster?
Fully-Firstable Anagram Sets
Today is the Center
Why is Collection not simply treated as Collection<?>
Took a trip to a parallel universe, need help deciphering
How to say in German "enjoying home comforts"
Stopping power of mountain vs road bike
Facing a paradox: Earnshaw's theorem in one dimension
Emailing HOD to enhance faculty application
How could indestructible materials be used in power generation?
In a Spin are Both Wings Stalled?
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?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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