-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Overridden ArrayList methods #1211
base: master
Are you sure you want to change the base?
Conversation
public void indexValidator(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new ArrayListIndexOutOfBoundsException("Index is not valid"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this method is not expected to be used outside this class, so make private.
don't forget to move it at the end of class, because of private.
public void indexValidator(int index) { | |
if (index < 0 || index >= size) { | |
throw new ArrayListIndexOutOfBoundsException("Index is not valid"); | |
} | |
} | |
private void indexValidator(int index) { | |
if (index < 0 || index >= size) { | |
throw new ArrayListIndexOutOfBoundsException("Index is not valid"); | |
} | |
} |
|
||
public ArrayList() { | ||
this.elementData = (T[]) new Object[DEFAULT_CAPACITY]; | ||
this.size = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You dont need to set size=0, because java makes the same instead of you.
this.size = 0; |
public Object[] grow(T[] elementData, int size) { | ||
if ((size + 1) >= elementData.length) { | ||
int newCapacity = elementData.length + (elementData.length >> 1); | ||
T[] newElementData = (T[]) new Object[newCapacity]; | ||
System.arraycopy(elementData, 0, newElementData, 0, size); | ||
elementData = newElementData; | ||
} | ||
return elementData; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think about proper access modifier for grow method.
} | ||
} | ||
|
||
public Object[] grow(T[] elementData, int size) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method grow should be void, because it changes the main array, e.g. this.elementData.
So, you can directly access this this.elementData
.
public Object[] grow(T[] elementData, int size) { | |
public void grow(T[] elementData, int size) { |
if (index < 0 || index > size) { | ||
throw new ArrayListIndexOutOfBoundsException("Index is not valid"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potentially repeating code should be extracted into private method.
if (index < 0 || index > size) { | |
throw new ArrayListIndexOutOfBoundsException("Index is not valid"); | |
} | |
if (index < 0 || index > size) { | |
throw new ArrayListIndexOutOfBoundsException("Index is not valid"); | |
} |
created addIndexValidator method
created addIndexValidator method
|
||
private void grow(T[] elementData, int size) { | ||
if ((size + 1) >= elementData.length) { | ||
int newCapacity = elementData.length + (elementData.length >> 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
binary operators reduces code readability. consider creating GROWTH_FACTOR = 1.5
constant
int newCapacity = elementData.length + (elementData.length >> 1); | |
int newCapacity = elementData.length * GROWTH_FACTOR; |
if (element == null) { | ||
for (int i = 0; i < size; i++) { | ||
if (elementData[i] == null) { | ||
return remove(i); | ||
} | ||
} | ||
} else { | ||
for (int i = 0; i < size; i++) { | ||
if (element.equals(elementData[i])) { | ||
return remove(i); | ||
} | ||
} | ||
} | ||
throw new NoSuchElementException("Element not found"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be more simple
if (element == null) { | |
for (int i = 0; i < size; i++) { | |
if (elementData[i] == null) { | |
return remove(i); | |
} | |
} | |
} else { | |
for (int i = 0; i < size; i++) { | |
if (element.equals(elementData[i])) { | |
return remove(i); | |
} | |
} | |
} | |
throw new NoSuchElementException("Element not found"); | |
for (int i = 0; i < size; i++) { | |
if (element == null ? elementData[i] == null : element.equals(elementData[i])) { | |
return remove(i); | |
} | |
} | |
throw new NoSuchElementException("Element not found"); |
for (int i = size; i > index; i--) { | ||
elementData[i] = elementData[i - 1]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No description provided.