ArrayList的一些用法

import java.util.Arrays;
public class MyArrayList<E> {
    private Object[] elements;
    private static final int DEFAULT_CAPACITY = 10;
    private static final int MAX_ARRAY_SIZE = 2147483639;
    private int size = 0;
    private boolean flag;
public MyArrayList() {
	this.elements = new Object[DEFAULT_CAPACITY];
	 }
 public MyArrayList(int initCapacity) {
        if (initCapacity >= 0 && initCapacity <= MAX_ARRAY_SIZE){
            this.elements = new Object[initCapacity];
            } else {
            throw new IllegalArgumentException
            ("IllegalArgumentException : " + initCapacity);
        }
    }
    
 public boolean add(E e) {
        return this.add(this.size, e);
    }    
    
 public boolean add(int index, E e) {
        if (index >= 0 && index <= this.size) {
            this.ensureCapacity(this.size + 1);
            for(int i = this.size; i > index; --i) {
                this.elements[i] = this.elements[i - 1];
            }
            this.elements[index] = e;
            ++this.size;
            return true;
             } else {
          throw new ArrayIndexOutOfBoundsException(index);
        }
    }
public boolean addAll(MyArrayList<? extends E> list) {
        Object[] array = list.toArray();
        int newSize = array.length;
        this.ensureCapacity(this.size + newSize);
	for(int i = 0; i < newSize; ++i) {
            this.elements[i + this.size] = array[i];
        }
        this.size += newSize;
        return true;
        }
        public boolean addAll(int index, MyArrayList<? extends E> list) {
        return true;
    }
    public boolean remove(Object obj) {
        int index = this.indexOf(obj);
        return this.remove(index) != null;
    }
    public E remove(int index) {
        if (-1 == index) {
		return null;
        } else {
            E e = this.get(index);
            for(int i = index; i < this.size - 1; ++i) {
                this.elements[i] = this.elements[i + 1];
            }
            this.elements[this.size - 1] = null;
            --this.size;
            return e;
           }
    }

还有其他的用法,等待下次更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值