ArrayList特点:
ArrayList和数组的区别:
常用方法介绍:
源码分析:
继承关系:
public class ArrayList < E> extends AbstractList < E>
implements List < E> , RandomAccess, Cloneable, java. io. Serializable
ArrayList底层数据结构是链表;
基本属性:
private static final int DEFAULT_CAPACITY = 10 ;
private static final Object[ ] EMPTY_ELEMENTDATA = { } ;
private transient Object[ ] elementData;
private int size;
构造函数
public ArrayList ( int initialCapacity) {
super ( ) ;
初始化数组大小
if ( initialCapacity < 0 )
throw new IllegalArgumentException ( "Illegal Capacity: " +
initialCapacity) ;
this . elementData = new Object [ initialCapacity] ;
}
public ArrayList ( ) {
super ( ) ;
this . elementData = EMPTY_ELEMENTDATA;
}
public ArrayList ( Collection< ? extends E > c) {
elementData = c. toArray ( ) ;
size = elementData. length;
if ( elementData. getClass ( ) != Object[ ] . class )
elementData = Arrays. copyOf ( elementData, size, Object[ ] . class ) ;
}
增长方式:按照原数组的1.5倍进行扩容;
CRUD方法研究(增删改查):
public boolean add ( E e) {
ensureCapacityInternal ( size + 1 ) ;
elementData[ size++ ] = e;
return true ;
}
private void ensureCapacityInternal ( int minCapacity) {
if ( elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math. max ( DEFAULT_CAPACITY, minCapacity) ;
}
ensureExplicitCapacity ( minCapacity) ;
}
private void ensureExplicitCapacity ( int minCapacity) {
modCount++ ;
if ( minCapacity - elementData. length > 0 )
grow ( minCapacity) ;
}
private void grow ( int minCapacity) {
int oldCapacity = elementData. length;
int newCapacity = oldCapacity + ( oldCapacity >> 1 ) ;
if ( newCapacity - minCapacity < 0 )
newCapacity = minCapacity;
if ( newCapacity - MAX_ARRAY_SIZE > 0 )
newCapacity = hugeCapacity ( minCapacity) ;
elementData = Arrays. copyOf ( elementData, newCapacity) ;
}
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;
}
public E get ( int index) {
rangeCheck ( index) ;
return elementData ( index) ;
}
private void rangeCheck ( int index) {
if ( index < 0 || index >= this . size)
throw new IndexOutOfBoundsException ( outOfBoundsMsg ( index) ) ;
}
E elementData ( int index) {
return ( E) elementData[ index] ;
}
自定义ArrayList
import java. util. Arrays;
import java. util. Iterator;
import java. util. ListIterator;
class ArrayList < E> implements Iterable < E> {
private E[ ] array;
private int size;
public ArrayList2 ( ) {
this . array= ( E[ ] ) new Object [ 10 ] ;
this . size= 0 ;
}
public boolean isFull ( ) {
if ( size< array. length)
return true ;
return false ;
}
private void grow ( ) {
this . array= Arrays. copyOf ( array, size+ ( size>> 1 ) ) ;
}
public boolean add ( E e) {
if ( isFull ( ) ) {
grow ( ) ;
}
this . array[ size++ ] = e;
return true ;
}
private boolean checkRange ( int i) {
if ( 0 < i&& i>= size)
throw new IndexOutOfBoundsException ( ) ;
return true ;
}
public E get ( int i) {
checkRange ( i) ;
return array[ i] ;
}
public boolean isEmpty ( ) {
if ( size== 0 )
return true ;
return false ;
}
private void moveLast ( int i) {
while ( i+ 1 < size) {
array[ i] = array[ i+ 1 ] ;
}
size-- ;
}
public boolean remove ( Object obj) {
if ( isEmpty ( ) )
return false ;
E num= ( E) obj;
for ( int i= 0 ; i< size; i++ ) {
if ( array[ i] . equals ( num) ) {
moveLast ( i) ;
return true ;
}
}
return false ;
}
@Override
public Iterator< E> iterator ( ) {
return new Itr ( 0 ) ;
}
private class Itr implements Iterator < E> {
private int ind;
protected Itr ( int start) {
this . ind= start;
}
@Override
public boolean hasNext ( ) {
if ( ind< size)
return true ;
return false ;
}
@Override
public E next ( ) {
if ( hasNext ( ) )
return get ( ind++ ) ;
throw new IndexOutOfBoundsException ( ) ;
}
@Override
public void remove ( ) {
if ( hasNext ( ) )
ArrayList2. this . remove ( ind) ;
throw new IndexOutOfBoundsException ( ) ;
}
}
}
public class ArrayListDemo {
public static void main ( String[ ] args) {
ArrayList< Integer> arrayList2 = new ArrayList < Integer> ( ) ;
ArrayList< Integer> arrayList1= new ArrayList < > ( ) ;
arrayList1. add ( 30 ) ;
arrayList1. add ( 12 ) ;
arrayList1. add ( 12 ) ;
arrayList2. add ( 12 ) ;
arrayList2. add ( 34 ) ;
arrayList2. add ( 12 ) ;
}
}
ArrayList迭代器元素详解以及自定义迭代器的实现 请点击
ArrayList求并集,交集,差集 请点击