我自己的arraylist

时间:2013-03-15 16:48:58

标签: java methods insert arraylist

我需要做自己的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++;
}

3 个答案:

答案 0 :(得分:5)

  1. temp的大小错误。由于您要添加单个元素,因此它应为array.length + 1

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

  3. 我没有得到array[index] != null支票。

  4. 最后,只需拨打一次Arrays.copyOfRange()即可替换if的整个正文。

答案 1 :(得分:0)

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

System.arraycopy(array, index - 1, array, index, array.length - index);

答案 2 :(得分: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;    

    }
}