码迷,mamicode.com
首页 > 编程语言 > 详细

JavaSE---集合

时间:2019-02-15 21:08:19      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:zab   turn   OLE   copy   size   new   一半   code   ase   

1、【ArrayList】

 

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
private static final long serialVersionUID = 8683452581122892189L; private static final int DEFAULT_CAPACITY = 10;   public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }   private void ensureCapacityInternal(int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); }   private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); }   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); }
  
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;
}

public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}

}


 

    初始容量:10   扩容:为原来的一半 int newCapacity = oldCapacity + (oldCapacity >> 1);

 

2、【HashMap】

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {
    
   static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
   static final int MAXIMUM_CAPACITY = 1 << 30;  
   static final float DEFAULT_LOAD_FACTOR = 0.75f;

}

    初始容量:16   

JavaSE---集合

标签:zab   turn   OLE   copy   size   new   一半   code   ase   

原文地址:https://www.cnblogs.com/anpeiyong/p/10385901.html

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