Hi, I get a "java.lang.UnsupportedOperationException at java.util.AbstractList.remove" My piece of code
List dataList = new ArrayList();
int dataSize = dataList.size();
for(int i = 0; i < dataSize; i++){
WorkflowProcess workflowProcessVO = (WorkflowProcess)dataList.get(i);
WorkflowProcess removedWorkFlowProcessVO = (WorkflowProcess)dataList.remove(i);
The remove method throws java.lang.UnsupportedOperationException at java.util.AbstractList.remove
Please let me know where I am wrong and what I should do to get rid of this exception.
Thanks a TON in advance.
-
java.lang.UnsupportedOperationException at java.util.AbstractLis (8 messages)
- Posted by: mathew lawrence
- Posted on: January 12 2006 01:47 EST
Threaded Messages (8)
- java.lang.UnsupportedOperationException at java.util.AbstractLis by D S on January 13 2006 09:54 EST
- java.lang.UnsupportedOperationException at java.util.AbstractLis by mathew lawrence on January 16 2006 23:06 EST
-
java.lang.UnsupportedOperationException at java.util.AbstractLis by D S on January 17 2006 04:37 EST
- java.lang.UnsupportedOperationException at java.util.AbstractLis by D S on January 17 2006 04:43 EST
- java.lang.UnsupportedOperationException at java.util.AbstractLis by Vijayaraj Marappa Gounder on April 20 2011 07:15 EDT
-
java.lang.UnsupportedOperationException at java.util.AbstractLis by D S on January 17 2006 04:37 EST
- java.lang.UnsupportedOperationException at java.util.AbstractLis by mathew lawrence on January 16 2006 23:06 EST
- Use LinkedList instead by Manfred Sch??rhoff on June 11 2008 05:15 EDT
- Use LinkedList instead by Manfred Sch??rhoff on June 11 2008 05:16 EDT
- Use LinkedList instead by Mike Moran on April 06 2010 19:23 EDT
-
java.lang.UnsupportedOperationException at java.util.AbstractLis[ Go to top ]
- Posted by: D S
- Posted on: January 13 2006 09:54 EST
- in response to mathew lawrence
My first question would be, is this the exact piece of code which causes the problem, or a piece of code that you put together to illustrate the problem? My next question would be, is the ArrayList here a java.util.ArrayList or another ArrayList which extends AbstractList but does not implement remove? The remove method on AbstractList is a stub which does not do anything - if you call it, it throws an UnsupportedOperationException. It relies on the subclass to implement the add and remove methods (because it is valid in some circumstances that you might want to implement a list which does not support remove). It looks to me as if whatever subclass you are using is not overriding this method. If you're not sure, add this line of code:
System.out.println(dataList.getClass().getName());
Incidentally, your code will fail with an IndexOutOfBoundsException anyway even if you solve the UnsupportedOperationException problem (try working through the for loop and watch what happens to the value of i and compare it to the number of entries remaining in the list after each iteration). -
java.lang.UnsupportedOperationException at java.util.AbstractLis[ Go to top ]
- Posted by: mathew lawrence
- Posted on: January 16 2006 23:06 EST
- in response to D S
Objective:
To remove the object from List
condition:
if the parameter passed to this method matches with the objects'(id) present in the arraylist
then delete the object from the list.
below is exact piece of code:
=============================
List dataList = new ArrayList();
//to convert object[] to List
dataList = Arrays.asList(data);
int dataSize = dataList.size();
String pitSysNo = "";
//this is the Value object
WorkflowProcess removedWorkFlowProcess;
if(recId != null){
//recId value passed as paramter to this method
recordId = Long.parseLong(recId);
}
for(int i = 0; i < dataSize; i++){
//GETTING the Value object from the list
WorkflowProcess workflowProcessVO = (WorkflowProcess)dataList.get(i);
//getting the value from the VO
sysNum = workflowProcessVO.getProcessDataPoint().getPitSysNo();
Long sysNumLong = new Long(sysNum);
pitSysNo = sysNumLong.toString();
//Checking if passed paramter to this method is null
if(recId != null){
//check if passed parameter matches with Value objects' value
if( recId.equals(pitSysNo)){
try{
removedWorkFlowProcess = (WorkflowProcess)dataList.remove(i);
}catch(UnsupportedOperationException uoexcep){
debug("Printing the exception"+uoexcep.getMessage());
}
}
}
}
I am not using AbstarctList here. I am using ArrayList only.
I printed the class name as you had told - here it is -
java.util.Arrays$ArrayList -
java.lang.UnsupportedOperationException at java.util.AbstractLis[ Go to top ]
- Posted by: D S
- Posted on: January 17 2006 04:37 EST
- in response to mathew lawrence
By the time you get to the remove(i) statement, list is no longer a java.util.ArrayList. When you call Arrays.asList it does not return a java.util.ArrayList. It returns a java.util.Arrays$ArrayList which is an immutable list. You cannot add to it and you cannot remove from it.
If you want a mutable list built from your array you will have to loop over the array yourself and add each element into the list in turn.
Even then your code won't work because you'll get an IndexOutOfBoundsException as you remove the elements from the list in the for loop. There are two options: use an Iterator which allows you to remove from the list as you iterate over it (my recommendation as it makes the code easier to maintain) or loop backwards over the loop removing from the last one downwards (harder to read). -
java.lang.UnsupportedOperationException at java.util.AbstractLis[ Go to top ]
- Posted by: D S
- Posted on: January 17 2006 04:43 EST
- in response to D S
By the way, you are using AbstractList. ArrayList and Arrays$ArrayList are both types of AbstractList. That's why you get UnsupportedOperationException: Arrays$ArrayList does not override remove(int) so the method is called on the superclass, AbstractList, which is what is throwing the exception because this method is not implemented on that class (the reason being to allow you to build immutable subclasses). -
java.lang.UnsupportedOperationException at java.util.AbstractLis[ Go to top ]
- Posted by: Vijayaraj Marappa Gounder
- Posted on: April 20 2011 07:15 EDT
- in response to mathew lawrence
Convert immutable java.util.Arrays$Arraylist to java.util.ArrayList
as
List<String> secAudienceLst = new ArrayList<String>();
secAudienceLst.addAll(Arrays.asList(blockAudience.split(","))); -
Use LinkedList instead[ Go to top ]
- Posted by: Manfred Sch??rhoff
- Posted on: June 11 2008 05:15 EDT
- in response to mathew lawrence
I was running into the UnsupportedOperationException as well. I found that using a LinkedList worked for me: List myUnitWrappers = new LinkedList(Arrays.asList((MawbUnitWrapper[]) request.getSession().getAttribute(MAINTAIN_MA_COST_ITEM_UNITS))); Since the linked list is growable/shrinkable, you can remove an object: myUnitWrappers.remove(index); -
Use LinkedList instead[ Go to top ]
- Posted by: Manfred Sch??rhoff
- Posted on: June 11 2008 05:16 EDT
- in response to mathew lawrence
I was running into the UnsupportedOperationException as well. I found that using a LinkedList worked for me: List myUnitWrappers = new LinkedList(Arrays.asList((MawbUnitWrapper[]) request.getSession().getAttribute(MAINTAIN_MA_COST_ITEM_UNITS))); Since the linked list is growable/shrinkable, you can remove an object: myUnitWrappers.remove(index); -
Use LinkedList instead[ Go to top ]
- Posted by: Mike Moran
- Posted on: April 06 2010 19:23 EDT
- in response to Manfred Sch??rhoff
It's not LinkedList that helped you. You could have used any List implementation that supports a modifyable result. This works because you instantiated a new modifiable list using the immutible list created from Arrays.aslist(...).