Search code examples
javahibernatelistarraylistunsupportedoperation

Getting UnsupportedOperationException when i remove elements at particular index in List<List<String>>?


I am having List<List<String>> data this way,

List<List<String>> repdata = [
    ["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","20","23840","FF20"],
    ["1192","R","11t","H","06","L","SA","04772","8345","2015","BZ C8 FE","01D6","13","33390","LC13"]]

I want to remove particularly, values at index 14 in the innerlists.

ex: in this inner list data

[["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","20","23840","FF20"]]

I want to remove FF20,

And this has to be repeated for all the inner lists in the List<List<String>> repdata.

So, my final List<List<String>> would be this way,

List<List<String>> repdata = [
["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","FF20","23840"],
["1192","R","11t","H","06","L","SA","04772","8345","2015","BZ C8 FE","01D6","13","33390"]

Actually this List<List<String>> repdata is = criteria.list() from hibernate. which is Transformed this way,

criteria.setResultTransformer(Transformers.TO_LIST);

I tired removing this way,

List<List<String>> repdata = cr.list();
        for( List<String> list: repdata ){
            if( list.size() > 14){                              
                list.remove(14);
            }           
        }

But i am continuously getting UnsupportedOperationException.

Can anyone help me in this issue?


Solution

  • Though I dont have enough information available, I suspect that the list returned is probably fixed size (or unmodifiable in some other way). One example is the list we get from Arrays.asList to which cannot add/remove anything.We can't structurally modify this List.

    The solution may be to initialize a list implementation using the items in current list. A LinkedList supports faster remove and may be suitable. Something like:

    List<String> list = new LinkedList<String>(Arrays.asList(your_list));
    

    You can also use ArrayList if it suits your requirement.