zoukankan      html  css  js  c++  java
  • java list.remove移除失败

     1. resultList.remove(i) 移除失败说明

      当i为Integer类型时,通过观察源码发现当找不到该类型时就会自动去找Object类型,即remove(object),因为集合中不存在对象类型的值,所以会造成失败。

    2. 解决方案

      resultList.remove(i.intValue())

    3. remove源码

     public E remove(int index) {
            rangeCheck(index);
    
            modCount++;
            E oldValue = elementData(index);
    
            int numMoved = size - index - 1;
            if (numMoved > 0)
                System.arraycopy(elementData, index+1, elementData, index,
                                 numMoved);
            elementData[--size] = null; // clear to let GC do its work
    
            return oldValue;
        }
     public boolean remove(Object o) {
            if (o == null) {
                for (int index = 0; index < size; index++)
                    if (elementData[index] == null) {
                        fastRemove(index);
                        return true;
                    }
            } else {
                for (int index = 0; index < size; index++)
                    if (o.equals(elementData[index])) {
                        fastRemove(index);
                        return true;
                    }
            }
            return false;
        }
    private void fastRemove(int index) {
            modCount++;
            int numMoved = size - index - 1;
            if (numMoved > 0)
                System.arraycopy(elementData, index+1, elementData, index,
                                 numMoved);
            elementData[--size] = null; // clear to let GC do its work
        }
  • 相关阅读:
    模拟电梯运行
    用户需求调研报告
    NABC需求分析
    大道至简---读书随笔3
    二维环形数组求最大子数组和
    结对开发之求最大数组溢出问题
    结对开发之环形数组
    结对开发之电梯调度
    我看“微软拼音”
    团队开发项目之典型用户和用户场景
  • 原文地址:https://www.cnblogs.com/jiangds/p/8177171.html
Copyright © 2011-2022 走看看