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
*/
@SuppressWa
JAVA集合类练习例题
于 2022-05-19 12:02:42 首次发布