import java.util.ConcurrentModificationException;
import java.util.NoSuchElementException;
public class ArrayList<E> implements Cloneable{
public static final class ArrayList1<E> implements Cloneable{
/**
* 此列表在结构上被修改的次数。
*/
private transient int modCount ;
int cursor;
public boolean hasNext() {
return cursor != size ;
}
@SuppressWarnings("unchecked")
private E next () {
int c = cursor ;
if ( c >= size)
throw new NoSuchElementException() ;
Object[] elementData = ArrayList1.this.elementData ;
if ( c >= elementData.length )
throw new ConcurrentModificationException() ;
if(cursor < elementData.length )
cursor = c +1 ;
return (E) elementData[c] ;
}
public ArrayList1 () {
this.elementData = ect ;
this.size = 0 ;
this.modCount = 0 ;
}
private static int hugeLength (int oldLength , int minGrowth ) {
int minLength = oldLength + minGrowth ;
if ( minLength < 0)
throw new OutOfMemoryError("所需的数组长度太大") ;
if (minLength <= MAX_ARRAY_LENGTH)
return MAX_ARRAY_LENGTH ;
return MAX_VALUE ;
}
public static int max(int a , int b) {
return (a >= b) ? a : b ;
}
private static final int MAX_VALUE = 0x7fffffff ;
private static final int MAX_ARRAY_LENGTH = MAX_VALUE - 8;
private int newLength ( int oldLength , int minGrowth , int prefGrowth ) {
int newLength = max(minGrowth , prefGrowth) + oldLength ;
if (newLength - MAX_ARRAY_LENGTH <= 0)
return newLength ;
else
return hugeLength(oldLength, minGrowth) ;
}
private static native Object newArray(Class<?> componentType ,
int length ) /*throws NegativeArraySizeException */;
private static Object newInstance (Class<?> componentType ,
int length) /*throws NegativeArraySizeException*/{
return newArray(componentType , length) ;
}
private static int min(int a , int b ) {
return (a <= b ) ? a : b ;
}
private static <T,U> T[] copyOf(U[] original ,
int newLength ,
Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class) ?
(T[])new Object[newLength] :
(T[]) newInstance(newType.getComponentType() , newLength) ;
System.arraycopy(original,0, copy,0, min(original.length , newLength));
return copy;
}
/**
* 后面的数据往前移动
* @param <T>
* @param original
* 一个数组
* @param newLength
* 数组的位置
* @return
*/
@SuppressWarnings("unchecked")
private static <T> T[] copyOf (T[] original , int newLength) {
return (T[])copyOf(original , newLength , original.getClass()) ;
}
private static final Object ect[] = {} ;
/**
* 默认初始容量。
*/
private static final int DEFA = 10 ;
/**
* 增加容量,以确保它至少可以容纳最小容量参数指定的元素数。
* @param minCapacity
* 所需的最小容量
* @return
*/
private Object[] grow(int minCapacity) {
int oldCapacity = elementData.length ;
if(oldCapacity > 0 || elementData != ect ) {
/*新数组的长度*/
int newCapacity = newLength(oldCapacity ,( minCapacity-oldCapacity) , oldCapacity>>1);
/*复制原数组到新数组*/
return elementData = copyOf(elementData , newCapacity) ;
} else {
return elementData = new Object[max(DEFA , minCapacity)] ;
}
}
/**
* 第一次所需的数组长度
* @return
* 一个数组
*/
private Object[] grow() {
return grow(size + 1) ;
}
private void add(E value , Object[] elementData , int size) {
if( size == elementData.length)
elementData = grow() ;
elementData[size] = value ;
this.size = size + 1 ;
}
/**
* ArrayList的大小(它包含的元素数)
*/
private transient int size ;
/**
* 存储ArrayList元素的数组缓冲区。ArrayList的容量是该数组缓冲区的长度。
*/
private transient Object[] elementData ;
/**
*
* @param value
* @return
*/
public boolean add (E value) {
if(value == null || value == "")
throw new NullPointerException("Null pointer error") ;
modCount++ ;
add(value , elementData , size) ;
return true ;
}
public static final int ME = 100 ;
public synchronized String toString() {
if (!hasNext())
return "[]" ;
StringBuilder s = new StringBuilder() ;
s.append("[") ;
for( ; ; ) {
E e = next() ;
if ( cursor <= ME ) {
if (e != null)
s.append( e == this ? "(this Collection)" : e );
}
if ( !hasNext()) {
if ( cursor >= ME) {
cursor = 0 ;
return s.append("......" + e + "]").toString() ;
} else {
cursor = 0 ;
return s.append("]").toString() ;
}
}
if ( cursor <= ME - 1 )
s.append(",").append(" ") ;
}
}
public Object clone() {
try {
ArrayList1<?> list = (ArrayList1<?>)super.clone() ;
list.elementData = copyOf(elementData,size) ;
return list ;
}catch(CloneNotSupportedException e) {
throw new InternalError(e) ;
}
}
/**
* 有没有我想要的值
* @param c
* 要搜索的值
* @param i
* 从这里开始
* @param u
* 数值的数量
* @return
* 如果找到我要的值 , 就返回整数值 ,否则为 -1 ;
*/
private int indexOfRange(Object c , int i , int u ) {
Object[] e = elementData ;
if ( c == null ) {
for ( int k = i ; k < u ; k++ ) {
if ( e[i] == null) {
return i ;
}
}
} else {
for ( int y = i ; y < u ; y++ ) {
if ( c.equals(e[y])) {
return y ;
}
}
}
return -1 ;
}
private int indexOf(Object c) {
return indexOfRange(c , 0 ,size ) ;
}
/**
* 如果此列表包含指定的元素,则返回true。
* @param o
* 要测试在此列表中存在的元素
* @return
* 如果此列表包含指定的元素,则为true
*/
public boolean contains( Object c) {
return indexOf(c) >= 0 ;
}
private void checkIndex(int index , int size ) {
if(index < 0 || index >= size)
throw new ArrayIndexOutOfBoundsException("Out of bounds") ;
}
/**
* 得到数组中的元素
* @param index
* 数组的位置
* @return
*/
@SuppressWarnings("unchecked")
private E encek (int index) {
return (E)elementData[index] ;
}
public E get(int index) {
checkIndex(index , size) ;
return encek(index) ;
}
private int indexOf ( Object o , int index , int size) {
Object k[] = elementData ;
if ( o == null )
throw new NullPointerException("Null pointer") ;
else {
for ( int t = index ; t < size ; t++ ) {
if ( k[t].equals(o)) {
return t ;
}
}
}
return -1 ;
}
public int index(Object i) {
return indexOf(i , 0 , size) ;
}
public boolean isEmpty () {
if ( elementData.equals(ect) )
throw new NullPointerException("Array undefined") ;
return size == 0 ;
}
private int lastIndexOfRange( Object o , int index ,int size ) {
Object[] g = elementData ;
if ( o == null || o =="")
throw new NullPointerException("Null pointer") ;
else {
for ( int y = size - 1 ; y > index ; y-- ) {
if ( o.equals(g[y]))
return y ;
}
}
return -1 ;
}
private int lastIndexOf ( Object o ) {
return lastIndexOfRange(o , 0 , size) ;
}
/**
* 删除元素在这里完成,并把后面的数据往前移动用以填补空缺的位置
* @param e
* 一个数组
* @param i
* 元素的位置
*/
private void fast(Object[] e , int i ) {
modCount ++ ;
final int newSize ;
if ( ( newSize = size - 1 ) > i ) {
System.arraycopy(e, i + 1 , e , i , newSize - i );
}
e[size = newSize ] = null ;
elementData = copyOf( elementData , newSize ) ;
}
@SuppressWarnings("unchecked")
public E romove ( int index ) {
checkIndex( index , size ) ;
final Object[] e = elementData ;
E old = (E) e[index] ;
fast( e , index ) ;
return old ;
}
public boolean remove( Object i ) {
Object k[] = elementData ;
final int g = size ;
int u = 0 ;
found : {
if ( i == null ) {
for ( ; u < g ; u++ ) {
if ( k[u] == null)
break found ;
}
} else {
for ( ; u < g ;u++ ) {
if ( k[u].equals(i))
break found ;
}
}
return false ;
}
fast( k , u ) ;
return true ;
}
/**
* 判断一个数值是否为空
* @param element
* 指定的数值
*/
private void nullelement ( E element) {
if ( element == null )
throw new NullPointerException("Null pointer") ;
}
public E set ( int index , E element) {
checkIndex(index, size );
nullelement(element) ;
E old = encek(index) ;
elementData[index] = element ;
return old ;
}
public int size () {
return size ;
}
public Object[] toArray () {
return copyOf(elementData,size) ;
}
public void clear () {
modCount ++ ;
final Object[] e = elementData ;
for (int t = size , i = size = 0 ; i < t ; i++ ) {
if ( e[i] != null)
e[i] = null ;
}
}
}
/**
* 从此列表中删除所有元素。此调用返回后,列表将为空。
*/
public void clear () {
list.clear();
}
/**
* 包含正确序列中此列表中所有元素的数组
* @return
* 返回一个数组,该数组按正确的顺序(从第一个元素到最后一个元素)包含此列表中的所有元素。
*/
public Object[] toArray () {
return list.toArray() ;
}
/**
* 此列表中的元素数
* @return
* 返回此列表中的元素数
*/
public int size () {
return list.size() ;
}
/**
* 用指定的元素替换此列表中指定位置的元素。
* @param index
* 数组的位置
* @param element
* 指定的元素
* @return
* 返回替换之前位于指定位置的元素
*/
public E set (int index , E element ) {
return list.set(index,element) ;
}
/**
* 从该列表中删除指定元素的第一个匹配项(如果存在)。如果列表中不包含该元素,它将保持不变。
* @param o
* 要从此列表中删除的元素(如果存在)
* @return
*/
public boolean remove ( Object t) {
return list.remove( t ) ;
}
/**
* 删除此列表中指定位置的元素。将任何后续元素向左移动(从它们的整数中减去一个)。
* @param i
* 指定的位置
* @return
*/
public E romove ( int i ) {
return list.romove(i) ;
}
/**
* 返回此列表中指定元素最后一次出现的索引,如果此列表不包含该元素,则返回-1
* @param o
* 指定的元素
* @return
*/
public int lastIndexOf( Object o ) {
return list.lastIndexOf(o) ;
}
/**
* 如果此列表不包含任何元素,则返回true。
* @return
*/
public boolean isEmpty () {
return list.isEmpty() ;
}
/**
* 返回此列表中第一次出现的指定元素的索引,如果此列表不包含该元素,则返回-1。
* @param i
* 指定的元素
* @return
*/
public int indexOf ( Object i) {
return list.index(i) ;
}
/**
* 返回此列表中指定位置的元素。
* @param index
* 指定的位置
* @return
*/
public E get(int index) {
return list.get(index) ;
}
/**
* 如果此列表包含指定的元素,则返回true。
* @param c
* 指定的元素
* @return
*/
public boolean contains ( Object c) {
return list.contains(c) ;
}
/**
* 返回此ArrayList实例的浅层副本。
*/
public Object clone() {
return list.clone() ;
}
public ArrayList() {
this.list = new ArrayList1<>() ;
}
/**
* 返回字符串的表示形式
*/
public synchronized String toString() {
return list.toString() ;
}
private transient final ArrayList1<E> list ;
/**
* 将指定的元素追加到此列表的末尾。
* @param e
* 要附加到此列表的元素
* @return
* true
*/
public boolean add(E value) {
return list.add(value) ;
}
}
import java.util.ConcurrentModificationException;
import java.util.NoSuchElementException;
集合调用 :
ArrayList<Object> list = new ArrayList<Object>() ;
for(int g = 0 ; g <= 10 ; g++)
/*将指定的元素追加到此列表的末尾。然后返回true*/
list.add("list" + g ) ;
list.add("list30") ;
/*如果此列表包含指定的元素,则返回true。*/
System.out.println(list.contains("list7"));
/*返回此列表中指定位置的元素。*/
System.out.println(list.get(1));
/*返回此列表中第一次出现的指定元素的索引,如果此列表不包含该元素,则返回-1。*/
System.out.println(list.indexOf("list70"));
/*如果此列表不包含任何元素,则返回true。*/
System.out.println(list.isEmpty());
/*返回此列表中指定元素最后一次出现的索引,如果此列表不包含该元素,则返回-1。*/
System.out.println(list.lastIndexOf("list30"));
/*删除此列表中指定位置的元素。将任何后续元素向左移动(从元素中减去一个)。*/
System.out.println(list.romove(3));
/*从该列表中删除第一个出现的指定元素(如果存在)。如果列表中不包含该元素,则该元素保持不变。*/
System.out.println(list.remove("list1"));
/*用指定的元素替换此列表中指定位置的元素。*/
System.out.println(list.set(3,"list8"));
/*返回此列表中的元素数。*/
System.out.println(list.size());
/*返回一个数组中的第一个元素到最后一个元素。*/
for ( Object i : list.toArray())
System.out.println(i);
/*从该列表中删除所有元素。*/
list.clear();
/*返回此ArrayList实例的浅层副本。*/
System.out.println(list.clone());