diff --git a/src/com/deepak/data/structures/Arrays/CustomArrayList.java b/src/com/deepak/data/structures/Arrays/CustomArrayList.java index f679beb..9cadfef 100644 --- a/src/com/deepak/data/structures/Arrays/CustomArrayList.java +++ b/src/com/deepak/data/structures/Arrays/CustomArrayList.java @@ -70,6 +70,21 @@ public T get(int index) { return (T)elementData[index]; } + /** + * Method to set an element in the list based on given index and a new value + * + * @param element + * @param index + */ + + public void set(E element, int index){ + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException("Invalid index passed !!"); + } + + elementData[index] = element; + } + /** * Method to remove the element at a given index * @@ -86,7 +101,8 @@ public Object remove(int index) { /* Starting from the index till last, move * each element to the left and decrease the size of list */ for (int i = index; i < size; i++) { - elementData[i] = elementData[i + 1]; + if(i + 1 < size) + elementData[i] = elementData[i + 1]; } size--; return removedElement;