• java数据结构2--集合总论


    集合类

    0.1、为什么出现集合类?

    面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式(容器)
    Java中集合也是类,真正用来存储东西的是某种集合类的实例对象。

    0.2、集合类 VS 数组

    数组和集合类都是容器,有何不同?集合类的特点

    0.3、Collection接口概述

    Collection 层次结构中的根接口。
    Collection 表示一组对象,这些对象也称为 collection 的元素。
    一些 collection 允许有重复的元素,而另一些则不允许。

    0.4接口中的方法

    添加操作
    boolean add(Object e)
    boolean addAll(Collection c)

    删除操作
    boolean remove(Object o)
    boolean removeAll(Collection c)

    查询操作
    int size()

    判断操作
    boolean isEmpty()
    boolean contains(Object obj)
    boolean containsAll(Collection c)

    0.5、继承父类的方法--迭代器

    Iterator iterator()

    说明:

    1. 迭代器不保证取出元素的顺序和存入的顺序一致,“有序”是靠集合实例本身保证的。
    2. 迭代器本身是一个接口,该方法返回的是一个迭代器实例对象,通常使用的是接口多态使用迭代器
    3. 迭代器中常用的两个方法是:

    boolean hasNext():判断是否有下一个元素
    Object next():取出下一个元素

     对ArrayList的方法进行分析

        /**
         * Returns an iterator over the elements in this list in proper sequence.
         *
         * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
         *
         * @return an iterator over the elements in this list in proper sequence
         */
        public Iterator<E> iterator() {
            return new Itr();
        }
    
        /**
         * An optimized version of AbstractList.Itr
         */
        private class Itr implements Iterator<E> {
            int cursor;       // index of next element to return
            int lastRet = -1; // index of last element returned; -1 if no such
            int expectedModCount = modCount;
    
            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;
                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();
                }
            }
    
            @Override
            @SuppressWarnings("unchecked")
            public void forEachRemaining(Consumer<? super E> consumer) {
                Objects.requireNonNull(consumer);
                final int size = ArrayList.this.size;
                int i = cursor;
                if (i >= size) {
                    return;
                }
                final Object[] elementData = ArrayList.this.elementData;
                if (i >= elementData.length) {
                    throw new ConcurrentModificationException();
                }
                while (i != size && modCount == expectedModCount) {
                    consumer.accept((E) elementData[i++]);
                }
                // update once at end of iteration to reduce heap write traffic
                cursor = i;
                lastRet = i - 1;
                checkForComodification();
            }
    
            final void checkForComodification() {
                if (modCount != expectedModCount)
                    throw new ConcurrentModificationException();
            }
        }
    Iterator

     

  • 相关阅读:
    旧题复习{6}
    CF219D. Choosing Capital for Treeland [树形DP]
    POJ1947 Rebuilding Roads[树形背包]

    洛谷P1280 尼克的任务[DP]
    NOIP2003pj栈[卡特兰数]
    NOIP2001统计单词个数[序列DP]
    洛谷P1415 拆分数列[序列DP 状态 打印]
    POJ2828 Buy Tickets[树状数组第k小值 倒序]
    CF380C. Sereja and Brackets[线段树 区间合并]
  • 原文地址:https://www.cnblogs.com/wqbin/p/11190739.html
Copyright © 2020-2023  润新知