Discussions

General J2EE: java.lang.UnsupportedOperationException at java.util.AbstractLis

  1. 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.
  2. 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).
  3. 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
  4. 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).
  5. 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).
  6. Convert immutable java.util.Arrays$Arraylist to java.util.ArrayList

    as

    List<String> secAudienceLst = new ArrayList<String>();
            secAudienceLst.addAll(Arrays.asList(blockAudience.split(",")));

  7. Use LinkedList instead[ Go to top ]

    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);
  8. Use LinkedList instead[ Go to top ]

    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);
  9. Use LinkedList instead[ Go to top ]

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