1

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?

Filburt
  • 17,626
  • 12
  • 64
  • 115
user1660325
  • 747
  • 4
  • 20
  • 35
  • 1
    Is it possible that `cr.list()` returns a fixed size list which cannot be modified? – akhil_mittal Sep 22 '15 at 08:51
  • I suspect `Criteria#list` returns an unmodifiable `List`, such as obtained by `Collections#unmodifiableList`. – Mena Sep 22 '15 at 08:52
  • See [here](http://stackoverflow.com/questions/715650/how-to-clone-arraylist-and-also-clone-its-contents) for instructions on how to create a deep copy of your `List`. – Mena Sep 22 '15 at 08:55
  • For loop and removal are a bad combination. Please use iterator or sth else mentioned at http://stackoverflow.com/questions/1196586/calling-remove-in-foreach-loop-in-java – mico Sep 22 '15 at 09:50
  • What does `repdata.getClass()` print? and so as the `list.getClass()` . That should give you a clue. – Karthik R Sep 22 '15 at 10:32

2 Answers2

1

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.

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
0

We shouldn't use the remove method of a List. Instead, traversing it using the iterator and using the iterator's own remove method is useful. Because when using list's remove method, ConcurrentModificationException will be thrown.