代码之家  ›  专栏  ›  技术社区  ›  PJT

Java:设置大小列表发布未支持的操作异常抛出

  •  1
  • PJT  · 技术社区  · 15 年前

    在Java中有一个设置大小的列表时,我遇到了一个问题。

    我知道我不能添加或从列表中删除,但为什么不能使用set? 当我使用set时,unsupportedperationexception将被抛出,当我使用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). 
    

    我知道这是一个可选操作,只是想用另一个元素替换列表中的一个元素。 我能做这个吗?

    编辑: 我正在使用LinkedList

    这是我问题的线索。

    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)
    
    4 回复  |  直到 15 年前
        1
  •  3
  •   Peter Lang    15 年前

    你可以从StackTrace上看到 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");
    

    你已经发现了 设置 是一个 optional operation 这个列表的实现不允许更改(如果构造为不可变的)。

        2
  •  0
  •   Uri    15 年前

    列表是一个接口。它(像collection)定义了可选操作。 可选操作是接口的一部分(为了保持一致性),但不能保证子类型会真正支持它们(这有点违反行为子类型)。换句话说,并不是所有列表的实际实现都必须支持这些操作,它们只需要记录是否支持这些操作。

    你实际使用的列表类型是什么?ArrayList?LinkedList?自定义类型?

    据我所知,arraylist确实支持set操作。 我不确定LinkedList 如果有自定义实现,则除非重写该方法,否则它可能不支持该实现。

        3
  •  0
  •   Lars Andren    15 年前

    List 不是类,它只是一个接口。你在实施你自己的计划吗 ?还是使用预定义的?

    如果是某个第三部分库实现 ,可能该实现根本不支持该操作(因此出现异常)。

        4
  •  0
  •   mdma    15 年前

    以下是一些修复建议

    1. 如果代码创建了原始列表,则更改构造函数调用,使其可变。
    2. 否则,如果 ITypeChecker.enter() 允许您替换列表引用,您可以创建可变列表并使用它,

    例如

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