您好,欢迎来到12图资源库!分享精神,快乐你我!我们只是素材的搬运工!!
  • 首 页
  • 当前位置:首页 > 开发 > WEB开发 >
    面试官让我聊聊 ArrayList 处置了数组的哪些成绩(3)
    时间:2020-09-17 21:23 来源:网络整理 作者:网络 浏览:收藏 挑错 推荐 打印

            int lastRet = -1; // index of last element returned; -1 if no such 

            int expectedModCount = modCount; 

            Itr() {}        // 假设游标不等于size 代表还有下一个元素        public boolean hasNext() {            return cursor != size

            }       //        @SuppressWarnings("unchecked"

            public E next() { 

                checkForComodification();            int i = cursor

                // 在此处判别以后游标是超过了数组有效元素个数 

                if (i >= size

                    throw new NoSuchElementException();            Object[] elementData = ArrayList.this.elementData;            //反省能否数组越界 

                if (i >= elementData.length) 

                    throw new ConcurrentModificationException();            cursor = i + 1; 

                // 更新lastRet游标为以后数组下标 

                return (E) elementData[lastRet = i]; 

            }        public void remove() {         // 反省删除元素下标能否合法 

                if (lastRet < 0) 

                    throw new IllegalStateException();            checkForComodification();            try {            // 删除原数组中制定下标元素 

                    ArrayList.this.remove(lastRet);                //更新游标 

                    cursor = lastRet;                lastRet = -1; 

                    expectedModCount = modCount;            } catch (IndexOutOfBoundsException ex) {                throw new ConcurrentModificationException();            }        }       } 

    (责任编辑:admin)