0

我需要做我自己的arraylist 类。我正在尝试编写插入方法,如果此索引中已经存在对象,我将面临将对象添加到索引 0 的问题。它适用于其他指数,但不适用于 0。任何帮助将不胜感激?

public void insert(int index, Object object) throws IndexOutOfBoundsException {

    if(index < 0) {
        System.out.println("Niepoprawny index!");
        throw new IndexOutOfBoundsException();
    }

    if(index > array.length - 1 || array[index] != null) {
        // zwiększenie rozmiaru tablicy
        // jeśli jest zbyta mała lub na danym miejscu istnieje jakiś obiekt
        Object[] temp = new Object[array.length + index];
        System.arraycopy(array, 0, temp, 0, array.length);
        array = temp;
    }

    // przesunięcie o 1 w prawo wszystkich elementów
    System.arraycopy(array, index - 1, array, index, array.length - index);
    array[index] = object;
    size++;
}
4

3 回答 3

5
  1. 的大小temp不对。由于您要添加单个元素,因此它应该是array.length + 1.

  2. 第二个索引arraycopy是错误的。

  3. 我不太明白array[index] != null支票。

最后,整个主体if都可以替换为对 的单个调用Arrays.copyOfRange()

于 2013-03-15T16:52:02.760 回答
0

使用这一行,您尝试在索引 -1 处插入数据:

System.arraycopy(array, index - 1, array, index, array.length - index);
于 2013-03-15T16:53:27.537 回答
0
import java.util.Arrays;

public class MyArrayList<E> {
    private int listSize = 0; // arraySize
    private int initialArraySize = 1;//initial size
    @SuppressWarnings("unchecked")
    private E[] data = (E[]) new Object[initialArraySize];//initialize array


    /**
     * auto increase array upon every added element
     * @return 
     */
    private  void IncreaseArraySize() {
        //increase the size of the array by one element when element is added
        data = Arrays.copyOf(data, data.length+1);
        }

    /**
     * add elements to myArrayList
     */

    public void add(E toBeAdded) {
        //add to array then change the array size by one element
        int i= data.length;
        if(listSize == data.length){
            IncreaseArraySize();
        }
        data[listSize] = toBeAdded;
        listSize++;
    }   

    /**
     * 
     * @param remove item from MyarrayList
     * 
     */
    public void remove(E ItemToRemove) {
        //remove the element you and then shrinks the array to new size after removal
        for (int i = 0; i < data.length; ++i) {
            if (data[i]==ItemToRemove) {
                for (int j = i; j < data.length-1; j++) {
                    data[j] = data[j + 1];
                }
            }
        }
        listSize--;
    }

    /**
     * to check if list contains the item
     */
    public boolean contains(E toCheck) {
        //iterate all elements of array while comparing element you want to the one in the index
        boolean b = false;
        for (int i = 0; i < data.length; i++) {
            if (data[i]==toCheck) {
                b = true;
            }
        }
        return b;
    }
/**
 * checks if list is empty
 */
    public boolean isEmpty() {
        //return false if list is not empty else true if if it is
        boolean b = false;
        if ((data.length == 1) && (data[0] == null)) {
            b = true;
        }
        return b;
    }

    /**
     * pass index you want to check contents
     */
    public E get(int index) {
        //make sure the index is not empty and not out of the array
        if (index < data.length && null != data[index]) {
            E ElementPos = data[index];
            return  ElementPos;
        } else if (index < 0 || index > data.length) {
            throw new IndexOutOfBoundsException();
        }
        return null;
    }

    /**
     * get the size of array
     */
    public int size() {
        return listSize;    

    }
}
于 2015-10-01T00:53:39.530 回答