gpt4 book ai didi

Java:设置大小列表问题抛出UnsupportedOperationException

转载 作者:行者123 更新时间:2023-11-29 06:21:47 24 4
gpt4 key购买 nike

我在更改 Java 中具有设定大小的列表时遇到问题。

我知道我不能在这个列表中添加或删除,但为什么我不能使用集合?当我使用 set 时抛出 UnsupportedOperationException 以及当我使用预期的添加和删除时。

设置

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)

最佳答案

你可以从你的堆栈跟踪中看到它,使用的 List 是一个 polyglot.util.TypedList ,它可以构造成不可变的。

set调用 tryIns,检查列表是否不可变 (see source):

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

正如您已经发现的那样,set 是一个可选操作,并且此 List 的实现不允许更改(如果构造为不可变的)。

关于Java:设置大小列表问题抛出UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2746026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com