tags:

views:

130

answers:

4

I am having a problem changing a List that has a set-size in Java.

I understand that I can't add or remove from this list but why can't I use set? When I use set the UnsupportedOperationException is thrown as well as when I use add and remove which is expected.

set

public Object set(int index,
                  Object element)

    Replaces the element at the specified position in this list with the specified element (optional operation). 

I understand its an optional operation its just that want to replace that one element of the list with another element. Is there any way I can do this?

EDIT: I am using a LinkedList

Here is the stack trace of my issue.

java.lang.UnsupportedOperationException: Add to an immutable TypedListIterator
    at polyglot.util.TypedList.tryIns(TypedList.java:195)
    at polyglot.util.TypedList.set(TypedList.java:148)
    at itype.visit.ItypeChecker.enter(ItypeChecker.java:114)
    at polyglot.visit.NodeVisitor.visitEdgeNoOverride(NodeVisitor.java:245)
    at polyglot.visit.NodeVisitor.visitEdge(NodeVisitor.java:217)
    at polyglot.ast.Node_c.visitChild(Node_c.java:173)
    at polyglot.ast.Node_c.visitList(Node_c.java:233)
    at polyglot.ast.ClassBody_c.visitChildren(ClassBody_c.java:63)
    at polyglot.visit.NodeVisitor.visitEdgeNoOverride(NodeVisitor.java:251)
    at polyglot.visit.NodeVisitor.visitEdge(NodeVisitor.java:217)
    at polyglot.ast.Node_c.visitChild(Node_c.java:173)
    at polyglot.ast.ClassDecl_c.visitChildren(ClassDecl_c.java:159)
    at polyglot.visit.NodeVisitor.visitEdgeNoOverride(NodeVisitor.java:251)
    at polyglot.visit.NodeVisitor.visitEdge(NodeVisitor.java:217)
    at polyglot.ast.Node_c.visitChild(Node_c.java:173)
    at polyglot.ast.Node_c.visitList(Node_c.java:233)
    at polyglot.ast.SourceFile_c.visitChildren(SourceFile_c.java:121)
    at polyglot.visit.NodeVisitor.visitEdgeNoOverride(NodeVisitor.java:251)
    at polyglot.visit.NodeVisitor.visitEdge(NodeVisitor.java:217)
    at polyglot.ast.Node_c.visit(Node_c.java:177)
    at polyglot.frontend.VisitorPass.run(VisitorPass.java:56)
    at polyglot.frontend.Scheduler.runPass(Scheduler.java:596)
    at polyglot.frontend.Scheduler.runGoal(Scheduler.java:499)
    at polyglot.frontend.Scheduler.attemptGoal(Scheduler.java:440)
    at polyglot.frontend.Scheduler.attemptGoal(Scheduler.java:412)
    at polyglot.frontend.Scheduler.attemptGoal(Scheduler.java:412)
    at polyglot.frontend.Scheduler.attemptGoal(Scheduler.java:412)
    at polyglot.frontend.Scheduler.attemptGoal(Scheduler.java:364)
    at polyglot.frontend.Scheduler.runToCompletion(Scheduler.java:297)
    at polyglot.frontend.Compiler.compile(Compiler.java:171)
    at polyglot.frontend.Compiler.compileFiles(Compiler.java:138)
    at polyglot.main.Main.start(Main.java:119)
    at polyglot.main.Main.start(Main.java:82)
    at polyglot.pth.SourceFileTest.invokePolyglot(SourceFileTest.java:162)
    at polyglot.pth.SourceFileTest.runTest(SourceFileTest.java:60)
    at polyglot.pth.AbstractTest.run(AbstractTest.java:32)
    at polyglot.pth.TestSuite.runTest(TestSuite.java:64)
    at polyglot.pth.ScriptTestSuite.runTest(ScriptTestSuite.java:55)
    at polyglot.pth.AbstractTest.run(AbstractTest.java:32)
    at polyglot.pth.Main.start(Main.java:41)
    at polyglot.pth.Main.main(Main.java:11)
A: 

List is an interface. It (like Collection) defines optional operations. Optional operations are part of the interface (for consistency) but there is no guarantee that subtypes would actually support them (it kind of violates behavioral subtyping). In other words, not all actual implementations of lists have to support these operations, they just have to document whether they do.

What actual list type are you using? ArrayList? LinkedList? A custom type?

For the best of my knowledge ArrayList DOES support the set operation. I am not sure about LinkedList If you have a custom implementation, it may not support it unless you override the method.

Uri
should not be rather a comment than answer?
Gabriel Ščerbák
@Gabirle: I was in the process of editing it into an answer.
Uri
A: 

List is not a class, it is just an interface. Are you implementing your own List? Or using a predefined one?

If it is some 3rd part library implementation of List, maybe that operation is simply not supported by that implementation (hence the exception).

Lars Andren
+2  A: 

You can see it from your stacktrace, the List used is a polyglot.util.TypedList, which can be constructed immutable.

set calls tryIns, which checks if the list is immutable (see source):

private  final void tryIns(Collection coll) {
  if (immutable)
    throw new UnsupportedOperationException JavaDoc(
                    "Add to an immutable TypedListIterator");

As you already found out set is an optional operation and this implementation of this List does not allow changes (if constructed immutable).

Peter Lang
My problem was fixed by creating a new TypedList and inserting all the elements from the old list into the new one along with the new elements that I wanted to add to the list or change.
tuckster
A: 

Here are some suggestions for fixes

  1. If your code creates the original list, then change the constructor call so that it is mutable.
  2. Otherwise, if the code in ITypeChecker.enter() allows you to replace the list reference you can create a mutable list and use that,

e.g.

List immutable = ....; // your immutable list
List mutable = new LinkedList(immutable);
// pass `mutable` to the rest of your code so the new list is used 
mdma