JAva集合のarrayList
ArrayList 。 , null。ArrayList 。size,isEmpty,get,set 。
add , n O(n) 。 。
ArrayList (Capacity), 。 , 。
, ensureCapacity ArrayList 。
まず、arrayListの一般的な方法について、彼について具体的な理解を持っています.public static void main(String[] args) {
//jdk1.5 , , list String
List<String> list=new ArrayList<String>();
//
list.add(0, " ");
list.add(" ");
// , , -1
System.out.println(list.indexOf(" "));
//
list.remove(1);
System.out.println(list);
list.add(" 2");
list.add(" 3");
// list
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
//
for(int i=(list.size()-1);i>=0;i--){
System.out.println(list.get(i));
}
// , String ,
// String
String str[]=list.toArray(new String[]{ });
System.out.println(" :");
for(int i=0;i<str.length;i++){
System.out.println(str[i]+";");
}
// , 。
Iterator<String> iter=list.iterator();
while(iter.hasNext()){
String str1=iter.next();
System.out.println(str1);
}
}
以上の方法からarrayListは配列中の要素の位置を検索したり、要素を増やしたり、要素を削除したりする機能をサポートしていることがわかりますが、彼の複雑さはどうでしょうか.次にarrayListのソースコードについて検討する
package java.util;
//arrayList util
//arrayList AbstractList, collection ,
//1. Serializable , ,
//2. RandomAccess , , ,
//3. Cloneable , 。
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L;
/**
*<span><span class="comment"> ArrayList , </span></span>
*
*/
private transient Object[] elementData;
/**
*
*
* @serial
*/
private int size;
/**
*
*
* @param initialCapacity the initial capacity of the list
* @exception IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
/**
* , 10
*/
public ArrayList() {
this(10);
}
/**
* <span><span class="comment"> collection ArrayList </span></span>
*
* @param c collection
* @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);
}
/**
* <span><span class="comment"> </span></span>
*/
public void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (size < oldCapacity) {
elementData = Arrays.copyOf(elementData, size);
}
}
/**
* <span><span class="comment"> ArrarList 。 </span><span> </span>
*<span class="comment"> ArrayList , =“( x3)/2 + 1” </span><span> </span></span>
*
* @param minCapacity the desired minimum capacity
*/
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
/**
* arrayList
*
* @return the number of elements in this list
*/
public int size() {
return size;
}
/**
* arrayList
*/
public boolean isEmpty() {
return size == 0;
}
/**
* arrayList
*
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
/**
* arrayList <pre name="code" class="java"> * , -1
*/
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;
}
<span>
</span>
/**
* Returns a shallow copy of this <tt>ArrayList</tt> instance. (The
* elements themselves are not copied.)
*
* @return a clone of this <tt>ArrayList</tt> instance
*/
public Object clone() {
try {
ArrayList<E> v = (ArrayList<E>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
<span> <span class="comment">// index
</span></span>
public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
}
<span> <span class="comment">// index element</span></span>
public E set(int index, E element) {
RangeCheck(index);
E oldValue = (E) elementData[index];
elementData[index] = element;
return oldValue;
}
<span><span class="comment">// e ArrayList </span></span>
// ensureCapacity()
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
//
public void add(int index, E element) {
// ,
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1);
<pre> //<span style="font-family:SimSun;font-size:14px;"><span style="color:red;"> </span><span style="font-size:12px;color:red;"> ,</span><span style="color:#ff0000;"><span style="font-size:12px;"> newlength ,
</span></span></span> //<span style="font-family:SimSun;font-size:14px;"><span style="color:#ff0000;"><span style="font-size:12px;"> System.arraycopy() </span>,<span style="font-size:12px;"> </span>。</span></span>
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
// , :1. ;2.
public E remove(int index) {
RangeCheck(index);
modCount++;
E oldValue = (E) elementData[index];
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
//
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
}
// arrayList
public void clear() {
modCount++;
// Let gc do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
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;
}
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;
}
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// Let gc do its work
int newSize = size - (toIndex-fromIndex);
while (size != newSize)
elementData[--size] = null;
}
private void RangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
// Write out array length
s.writeInt(elementData.length);
// Write out all elements in the proper order.
for (int i=0; i<size; i++)
s.writeObject(elementData[i]);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
<span><span class="comment">// java.io.Serializable : </span><span> </span></span><span>
<span class="comment">// ArrayList “ ” , “ ”
</span></span><span><span></span></span>
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in size, and any hidden stuff
s.defaultReadObject();
// Read in array length and allocate array
int arrayLength = s.readInt();
Object[] a = elementData = new Object[arrayLength];
// Read in all elements in the proper order.
for (int i=0; i<size; i++)
a[i] = s.readObject();
}
}