首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IndexOutOfBoundsException in LinkedList

IndexOutOfBoundsException in LinkedList
EN

Stack Overflow用户
提问于 2020-03-24 15:22:19
回答 1查看 88关注 0票数 0

我得到了IndexOutOfBoundsException,这在我看来是不可能的。

代码:

代码语言:javascript
复制
public class SomethingCalculator {

    @Nullable
    private Config mConfig;
    @Nullable
    private Long mTime;
    final private List<Long> mLinkedList = new LinkedList<>();


    public synchronized void setupWithConfiguration(Config config, Long time) {
        //config and time are non null always
        mTime = time;
        mConfig = config;
        generatLookUp();
    }


    public synchronized void reset() {
        mConfig = null;
        mTime = null;
        mLinkedList.clear();
    }


    @Nullable
    public synchronized Long getTheValue(long ms) {
        if (mConfig == null)
            return null;

        // getting exception here
        if (ms > mLinkedList.get(mLinkedList.size() - 1)) {
            return 0l; // something
        }
        return 0l; // something
    }


    private synchronized void generatLookUp() {
        mLinkedList.clear();
        if (mConfig == null || mTime==null)
            return;
        // mLinkedList will always have size > 0 after executing this
        // based upon config this method add elements to mLinkedList

        // adding dummy values
        mLinkedList.add(1L);
        mLinkedList.add(2L);
    }
}

调用SomethingCalculator.getTheValue()时获取异常

异常:

代码语言:javascript
复制
Fatal Exception: java.lang.IndexOutOfBoundsException: Index: 4, Size: 0
 at java.util.LinkedList.checkElementIndex(LinkedList.java:565)

不确定mMylinkedList.size()是否返回4,那么mMylinkedList.get(4)如何抛出IndexOutOfBoundsException。

EN

回答 1

Stack Overflow用户

发布于 2020-12-10 11:50:40

在查看生成的AAR之后,发现synchronized关键字已被删除,因此可以发生此异常。由于程序保护优化,synchronized关键字被删除。

删除了以下内容:

代码语言:javascript
复制
-optimizations !method/marking/synchronized

现在,我可以在生成的AAR中看到synchronized关键字。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60834009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档