在Java中创建类模拟ArrayList

在Java中创建类模拟ArrayList,java,arraylist,Java,Arraylist,我试图创建一个类来模拟一些ArrayList行为 在尝试增加容量或原始阵列时,我使用了int[]arr1存储阵列的元素和int[]tempArr临时存储arr1 这不管用。那么你认为我可以使用的另一种方法是什么呢 这是我的代码:请参见public void add public class ArrayListLike implements ArrayListLikeInterface { public int capacity = 0; // The capacity of the

我试图创建一个类来模拟一些
ArrayList
行为

在尝试增加容量或原始阵列时,我使用了
int[]arr1
存储阵列的元素和
int[]tempArr
临时存储
arr1

这不管用。那么你认为我可以使用的另一种方法是什么呢

这是我的代码:请参见
public void add

public class ArrayListLike implements ArrayListLikeInterface {


    public int capacity = 0; // The capacity of the array list (ArrayListLike)
    private static final int INIT_CAPACITY = 10; // Initial capacity
    private int size = 0;  // Initial size
    private int[] arr;
    private int[] tempArr;
    private int count = 0;


    // Constructors
    public ArrayListLike() {
        arr = new int[INIT_CAPACITY];
        capacity = INIT_CAPACITY;
    }



    public void add(Integer element) {
        // TODO Auto-generated method stub
        if (count == capacity) {
            tempArr = new int[capacity];
            System.arraycopy(arr, 0, tempArr,0, capacity);
            capacity *= 2;
            arr = null;
            arr = new int[capacity];
            System.arraycopy(tempArr, 0, arr,0, capacity);
            arr[count] = element;
            count++;
        } else {
            arr[count] = element;
            count++;
        }
    }

    public int get(int index) {
        // TODO Auto-generated method stub
        if (index <=  capacity) {
            return arr[index];
        } else {
            throw new ArrayIndexOutOfBoundsException(index);
        }
    }

    public int size() {
        // TODO Auto-generated method stub
        size = arr.length;
        return size;
    }


}
下面是错误:

Exception in thread "main" 10
java.lang.ArrayIndexOutOfBoundsException
    at java.base/java.lang.System.arraycopy(Native Method)
    at hw5sol.ArrayListLike.add(ArrayListLikeInterface.java:31)
    at hw5sol.Main.main(Main.java:21) 

将旧阵列复制到新阵列时,使用新的容量大小,即20,并且
temp
只有10个元素,因此在尝试复制下一个元素时会抛出

public void add(Integer element) {
    // TODO Auto-generated method stub
    if (count == capacity) {
        tempArr = new int[capacity];
        System.arraycopy(arr, 0, tempArr,0, capacity);
        capacity *= 2;
        arr = null;
        arr = new int[capacity];
//Here!!
        System.arraycopy(tempArr, 0, arr,0, tempArr.length);
        arr[count] = element;
        count++;
    } else {
        arr[count] = element;
        count++;
    }
}

什么不起作用?它给你带来了一个例外?请详细说明如果您想模拟
ArrayList
第一件事就是实现相同的接口@mckuok我添加了主类和错误。@OlegSklyar我不是在尝试模仿一切,我也在尝试用我自己的接口从头开始模仿。至少你在临时数组中放入了N个元素,然后你试图从同一数组中检索2N到新扩展的空间中
public void add(Integer element) {
    // TODO Auto-generated method stub
    if (count == capacity) {
        tempArr = new int[capacity];
        System.arraycopy(arr, 0, tempArr,0, capacity);
        capacity *= 2;
        arr = null;
        arr = new int[capacity];
//Here!!
        System.arraycopy(tempArr, 0, arr,0, tempArr.length);
        arr[count] = element;
        count++;
    } else {
        arr[count] = element;
        count++;
    }
}