【发布时间】:2011-02-14 07:39:00
【问题描述】:
我在更改具有 Java 中设置大小的列表时遇到问题。
我知道我无法在此列表中添加或删除,但为什么我不能使用 set? 当我使用 set 时抛出 UnsupportedOperationException 以及当我使用预期的 add 和 remove 时。
设置
public Object set(int index,
Object element)
Replaces the element at the specified position in this list with the specified element (optional operation).
我理解它是一个可选操作,它只是想用另一个元素替换列表中的那个元素。 有什么办法可以做到这一点吗?
编辑: 我正在使用链接列表
这是我的问题的堆栈跟踪。
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)
【问题讨论】:
-
java.util.List是一个接口。您使用的是哪种实现方式? -
听起来你在使用Collections.unmodifiableList,它是不可修改的,不仅在大小上而且在内容上。
-
根据 Sun 的说法,LinkedList 应该支持该操作。
-
其实有办法查出它是什么类型的List吗?因为我最初将它设置为 LinkedList 但后来我将其设置为另一个列表(我不确定的类型)
-
@tuckster:我认为您可以通过发布异常的堆栈跟踪来回答所有问题(好吧,不是全部)。要找出列表的类型
System.out.println(yourList);应该会给你信息。