JAVA集合类练习例题

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值