UnsupportedOperationException at java.util.AbstractList.remove

Hi,

The problem is that I keep getting this

"UnsupportedOperationException at java.util.AbstractList.remove"

exception.

I am not using ArrayList anywhere in my program.

I have 2 lists, I want the intersection of these 2 lists.

The error occurs at first remove the program encounters. So it can't remove this item. Both lists have at least one String item (I checked using System.out ...).

This is the code:

public List<String> isfreq(List<Edge> currsubg, Edge newEdge) {

List<Edge> csg = currsubg; //Edge is some datatype

Edge edge = newEdge;

List<String> namesUntilNow = edge.getListGraphNames(); //returns list of String

List<String> newNames = new LinkedList<String>(); //emptylist

//Take intersection of an array of Edge (each having a list of String)

for (int i = 0; i < csg.size(); i++){

Edge temp = csg.get(i).cloneIt();

List<String> namesThisEdge = temp.getListGraphNames();

newNames = listIntersection(namesThisEdge, namesUntilNow);

namesUntilNow = newNames;

}

return namesUntilNow;

}

//Take intersection of two string lists

public List<String> listIntersection(List<String> arrayOne, List<String> arrayTwo){

List<String> one = arrayOne;

List<String> two = arrayTwo;

List<String> out = new LinkedList<String>();

int test = 0;

while (!(one.isEmpty() || two.isEmpty())){

test = one.get(0).compareTo(two.get(0));

//Equal

if (test == 0){

out.add(one.get(0));

one.remove(0); <-- error occurs directly here

two.remove(0);

}

...

}

previously the one.remove worked just fine. listIntersection has not changed.

Any help would be greatly appreciated. Thanks.

[1892 byte] By [Kim_Kima] at [2007-11-27 6:02:27]
# 1
What type of List is returned by temp.getListGraphNames()?It would appear that the List it returned does not support the optional remove function. Perhaps it is an unmodifiable list.
jbisha at 2007-7-12 16:43:55 > top of Java-index,Java Essentials,Java Programming...
# 2

the return type is a list of String. This List of String has been created as an empty list (like this: List<String> listGraphName = new LinkedList<String>();) and then adding Strings to it using a loop and the standard list operation 'add'.

The code completion does give me the option to use 'remove' there. So 'remove' should be supported then.

It does indeed appear to be an unmodifiable list. 'add' also does not work. But then why is it suddenly an unmodifiable list?

This worked fine before, but now it does not anymore.

Thanks

Kim_Kima at 2007-7-12 16:43:55 > top of Java-index,Java Essentials,Java Programming...
# 3

> The code completion does give me the option to use

> 'remove' there. So 'remove' should be supported

> then.

No, remove() is an optional operation. Code completion has nothing to say in that matter.

> It does indeed appear to be an unmodifiable list.

> 'add' also does not work. But then why is it suddenly

> an unmodifiable list?

It's possibly converted at some point. Print out list.getClass() and see what the actual class of the list is.

-Kayaman-a at 2007-7-12 16:43:55 > top of Java-index,Java Essentials,Java Programming...
# 4

> It does indeed appear to be an unmodifiable list.

> 'add' also does not work. But then why is it suddenly

> an unmodifiable list?

Gremlins.

>

> This worked fine before, but now it does not

> anymore.

You could create a new modifiable List (not just a reference to the old) from the passed, unmodifiable List.

jbisha at 2007-7-12 16:43:55 > top of Java-index,Java Essentials,Java Programming...