码迷,mamicode.com
首页 > 其他好文 > 详细

学习list

时间:2018-04-20 11:41:26      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:range   copy   details   length   扩容   指定元素   ini   构造方法   http   

 

一.集合的基本结构图

技术分享图片技术分享图片

collection 的方法

 

add(E element) 确保此 collection 包含指定的元素。 
addAll(Collection<? extends E> c) 将指定 collection 中的所有元素都添加到此 collection 中。
clear() 从collection中移除所有元素。
contains(Object o) 如果collection包含指定的元素,则返回 true。
containsAll(Collection<?> c) 如果collection包含指定 collection 的所有元素,则返回 true。
equals(Object o)  比较指定的对象与此 collection 是否相等。
hashCode()  返回此collection的哈希码值。
isEmpty()  如果此 collection 不包含元素,则返回 true。
remove(Object o)  从此 collection 中移除指定元素的单个实例,如果存在的话。
removeAll(Collection<?> c) 移除此 collection 中那些也包含在指定 collection 中的所有元素。
retainAll(Collection<?> c) 仅保留此 collection 中那些也包含在指定 collection 的元素。
size() 返回此 collection 中的元素数。
toArray() 返回包含此 collection 中所有元素的数组。
toArray(T[] a)  返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。
iterator() 返回在此 collection 的元素上进行迭代的迭代器。(继承的方法)

 

 

 

 

二.list中提供的方法

 

继承collection的所有方法 把其中的collection改成列表,大致意思和用法不变。变的方法在下面重写。
add(E element) 向列表的尾部添加指定的元素。 
add(int index,E element) 在列表的指定位置插入指定元素。将当前处于该位置的元素(如果有的话)和所有后续元素向右移动(在其索引中加 1)。
addAll(int index,Collection<? extends E> c) 将指定 collection 中的所有元素都插入到列表中的指定位置。                                  
get(int index)  返回列表中指定位置的元素。
indexOf(Object o)   返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。 
lastIndexOf(Object o)  返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。
remove(int index)  移除列表中指定位置的元素。
remove(Object o) 从此列表中移除第一次出现的指定元素(如果存在)。
set(int index, E element) 用指定元素替换列表中指定位置的元素。
subList(int fromIndex, int toIndex) 返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。 
toArray()   返回按适当顺序包含列表中的所有元素的数组(从第一个元素到最后一个元素)。 
toArray(T[] a)  同collection, 且按适当顺序排列。
iterator()  返回按适当顺序在列表的元素上进行迭代的迭代器。 
listIterator()  返回此列表元素的列表迭代器(按适当顺序)。 
listIterator(int index)  返回列表中元素的列表迭代器(按适当顺序),从列表的指定位置开始。 

 

 

 

 

 

 

 

 

 

 

 

 

 

三.下面说一说Aarraylist和linkedlist

  1.AarrayList:

    对于ArrayList而言,它实现List接口、底层使用数组保存所有元素。其操作基本上是对数组的操作。下面我们来分析ArrayList的源代码:

  1)底层使用数组实现

  

private transient Object[] elementData; 

   2)构造方法:

ArrayList提供了三种方式的构造器,可以构造一个默认初始容量为10的空列表、构造一个指定初始容量的空列表以及构造一个包含指定collection的元素的列表,这些元素按照该collection的迭代器返回它们的顺序排列的。

public ArrayList() {  
    this(10);  
}  
  
public ArrayList(int initialCapacity) {  
    super();  
    if (initialCapacity < 0)  
        throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);  
    this.elementData = new Object[initialCapacity];  
}  
  
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);  

3)存储

ArrayList提供了set(int index, E element)、add(E e)、add(int index, E element)、addAll(Collection<? extends E> c)、addAll(int index, Collection<? extends E> c)这些添加元素的方法。下面我们一一讲解:

// 用指定的元素替代此列表中指定位置上的元素,并返回以前位于该位置上的元素。  
public E set(int index, E element) {  
    RangeCheck(index);  
  
    E oldValue = (E) elementData[index];  
    elementData[index] = element;  
    return oldValue;  
}  
// 将指定的元素添加到此列表的尾部。  
public boolean add(E e) {  
    ensureCapacity(size + 1);   
    elementData[size++] = e;  
    return true;  
}  
// 将指定的元素插入此列表中的指定位置。  
// 如果当前位置有元素,则向右移动当前位于该位置的元素以及所有后续元素(将其索引加1)。  
public void add(int index, E element) {  
    if (index > size || index < 0)  
        throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);  
    // 如果数组长度不足,将进行扩容。  
    ensureCapacity(size+1);  // Increments modCount!!  
    // 将 elementData中从Index位置开始、长度为size-index的元素,  
    // 拷贝到从下标为index+1位置开始的新的elementData数组中。  
    // 即将当前位于该位置的元素以及所有后续元素右移一个位置。  
    System.arraycopy(elementData, index, elementData, index + 1, size - index);  
    elementData[index] = element;  
    size++;  
}  
// 按照指定collection的迭代器所返回的元素顺序,将该collection中的所有元素添加到此列表的尾部。  
public boolean addAll(Collection<? extends E> c) {  
    Object[] a = c.toArray();  
    int numNew = a.length;  
    ensureCapacity(size + numNew);  // Increments modCount  
    System.arraycopy(a, 0, elementData, size, numNew);  
    size += numNew;  
    return numNew != 0;  
}  
// 从指定的位置开始,将指定collection中的所有元素插入到此列表中。  
public boolean addAll(int index, Collection<? extends E> c) {  
    if (index > size || index < 0)  
        throw new IndexOutOfBoundsException(  
            "Index: " + index + ", Size: " + size);  
  
    Object[] a = c.toArray();  
    int numNew = a.length;  
    ensureCapacity(size + numNew);  // Increments modCount  
  
    int numMoved = size - index;  
    if (numMoved > 0)  
        System.arraycopy(elementData, index, elementData, index + numNew, numMoved);  
  
    System.arraycopy(a, 0, elementData, index, numNew);  
    size += numNew;  
    return numNew != 0;  
} 

https://blog.csdn.net/u010305706/article/details/51007826

 

学习list

标签:range   copy   details   length   扩容   指定元素   ini   构造方法   http   

原文地址:https://www.cnblogs.com/strugglecola/p/8889216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!