Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handled an index out of bounds case when removing a valid element fro… #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/com/deepak/data/structures/Arrays/CustomArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
Expand Down