一天一个类--ArrayList之二

继续我的小激动~~~

1、看看构造一个ArrayList 有两种方式

一个指定大小,一个不指定。我们知道他其实使用数组来实现了,数组肯定要有大小,那么他没指定大小,默认的是多少呢???追踪源码---开启万里追踪模式~~~

   /**
     * Constructs an empty list with an initial capacity of ten.
     */
    public ArrayList() {
        this(10);
    }

知道了,就是10~~。

2、还有一个通过现有的集合类来实现构造。怎么实现的啊???

   /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection‘s
     * iterator.
     *
     * @param c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        size = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
    }

原来是调用Arrays.copyOf() 这个工具类。

3、插入元素add()

  看起来好简单的方法,插入就行了呗。

诸位看官,有没有考虑,插不进去咋办?满了!~

    /**
     * Increases the capacity to ensure that it can hold at least the
     * number of elements specified by the minimum capacity argument.
     *
     * @param minCapacity the desired minimum capacity
     */
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

int newCapacity = oldCapacity + (oldCapacity >> 1); 

动态扩容,很显然变为了原来的1.5倍。【在JDK6中 int newCapacity = oldCapacity *3/2+1;】

4、查找 indexOf()

  其实还不是一个一个的遍历!!但是不要忘了咱们的ArrayList可以存null的~

  这就要分类了,看看源码的

    /**
     * Returns the index of the first occurrence of the specified element
     * in this list, or -1 if this list does not contain the element.
     * More formally, returns the lowest index <tt>i</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
     * or -1 if there is no such index.
     */
    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

  如果是null 就遍历所有的,使用==

  如果不是,还要遍历 使用equals

  【从这里我们就知道了,二者是不一样的】哇塞!~

5、clone / toArray()

  怎么克隆?? 还是Arrays.copyOf()!!!

6、在指定位置插入元素 add(int index, E elements)

  咋个实现?挪呗!~index 后面的元素,乖乖的向后走一个,给我个地方~!

   public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }

  现在知道删除remove(index)是如何实现的了吧?但是挪了之后,最后一个元素怎么处理??置null让gc自己处理。不信自己看源码!~

  那么,删除指定元素呢?(在我们ArrayList中指的是删除第一个出现的元素。)如果这个元素是null ? 你可依然记得indexOf()里面怎么实现的(== 和 equals) ,yes!还是分两种情况查找,删除!

7、clear()

  全部元素置null

这个ArrayList 就是这么多了 ~ 么么嗒

转载请注明:http://home.cnblogs.com/u/plxx/

时间: 2024-08-07 01:14:45

一天一个类--ArrayList之二的相关文章

一天一个类--ArrayList之一

今天开始打算将JDK7种的一些类的源码分析一下,笔者认为了解源码就是了解其实现过程,这是非常重要的,而不是简单的记住方法的使用,关键是了解其思想和目的这才是重要的.所以笔者决定首先将从一些容器下手.[好欺负^_^] 位于顶层的是Collection,这个是一个接口,就是“祖宗”啊- 具体介绍可以参见API 常见的方法: boolean add(E e) Ensures that this collection contains the specified element (optional op

JavaSE入门学习35:Java集合框架之List接口及其实现类ArrayList和LinkedList

一List接口概述 List接口是Collection接口的子接口,实现List接口的集合类中的元素是有顺序的,而且可以重复,被称为序列. List集合中的元素都对应一个整数型的序列容器中的序号记载其在容器中的位置,可以根据序号存取容器中的元 素.List接口可以精确的控制每个元素的插入位置,或者删除某个位置元素. Java所提供的List集合实现类类有ArrayList实现类.LinkedList实现类.Vector等,我们主要使用的是ArrayList实 现类和LinkedList实现类.

java学习笔记--类ArrayList和LinkedList的实现

在集合Collection下的List中有两个实现使用的很频繁,一个是ArrayList,另一个是LinkedList,在学习中肯定都会有这样的疑问:什么时候适合使用ArrayList,什么时候用LinkedList?这时,我们就需要了解ArrayList和LinkedList的底层的实现,下面,为了更好的了解它们具体是怎样实现的,我们来写自己的ArrayList 和LinkedList. ArrayList底层是基于数组实现的,数组在内存中是存储在连续的存储单元中,在数据查找的时候比较快,适用

C++ 嵌套类使用(二)

C++嵌套类 1.   嵌套类的名字只在外围类可见. 2.   类的私有成员只有类的成员和友元可以访问,因此外围类不可以访问嵌套类的私有成员.嵌套类可以访问外围类的成员(通过对象.指针或者引用). 3.   一个好的嵌套类设计:嵌套类应该设成私有.嵌套类的成员和方法可以设为 public . 4.   嵌套类可以直接访问外围类的静态成员.类型名( typedef ).枚举值. // qiantaolei.cpp : Defines the entry point for the console

类和对象(二)

对象的自身引用是面向对象程序设计语言中特有的.十分重要的一种机制.在C++中,为这种机制设立了专门的表示:this指针变量. 在类的每一个成员函数的形参表中都有一个隐含的指针变量this,该指针变量的类型就是成员函数所属类的类型. 当程序中调用类的成员函数时,this指针变量被自动初始化为发出函数调用的对象的地址. 例如: 123456789101112131415161718 #include <iostream>using namespace std;class Sample{ int x

Spring源码分析——BeanFactory体系之抽象类、类分析(二)

上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry——Spring源码分析——BeanFactory体系之抽象类.类分析(一),今天继续分析. 一.工厂Bean注册支持——FactoryBeanRegistrySupport 废话不多说,直接看我注释的源码: /* * Copyright 2002-2012 the original author or authors. * * Licensed und

java 常用工具类的使用&lt;二&gt;

一.String工具类 1 package com.mkyong.common; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * 8 * String工具类. <br> 9 * 10 * @author 宋立君 11 * @date 2014年06月24日 12 */ 13 public class StringUtil { 14 15 private static final int INDEX_NOT

【FastDev4Android框架开发】RecyclerView完全解析之打造新版类Gallery效果(二十九)

转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/49946589 本文出自:[江清清的博客] (一).前言: 话说RecyclerView已经面市很久,也在很多应用中得到广泛的使用,在整个开发者圈子里面也拥有很不错的口碑,那说明RecyclerView拥有比ListView,GridView之类控件有很多的优点,例如:数据绑定,Item View创建,View的回收以及重用等机制.本系列文章会包括到以下三个部分: R

java的List接口的实现类 ArrayList,LinkedList,Vector 的区别

Java的List接口有3个实现类,分别是ArrayList.LinkedList.Vector,他们用于存放多个元素,维护元素的次序,而且允许元素重复. 3个具体实现类的区别如下: 1. ArrayList是最常用的List实现类,内部是通过数组实现的,它允许对元素进行快速随机访问.数组的缺点是每个元素之间不能有间隔,当数组大小不满足时需要增加存储能力,就要将已经有数组的数据复制到新的存储空间中.当从ArrayList的中间位置插入或者删除元素时,需要对数组进行复制.移动.代价比较高.因此,它