-
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
created own ArrayList collection which implements our List interface … #1216
base: master
Are you sure you want to change the base?
Conversation
…and uses custom exeption
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.
Before you create PR with your homework, you need to go through the checklist under the task and correct all the points described there. The mentor will not check the work until the checklist points are corrected
this.elements = new Object[DEFAULT_CAPACITY]; | ||
} | ||
|
||
private void ensureCapacity() { |
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.
Private ones (methods, classes) should be after public ones.
private void ensureCapacity() { | ||
if (size == elements.length) { | ||
int newCapacity = (int) (elements.length * 1.5); | ||
elements = Arrays.copyOf(elements, newCapacity); |
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.
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.
sorry for my inattention
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.
Good job overall!
public class ArrayList<T> implements List<T> { | ||
private static final int DEFAULT_CAPACITY = 10; | ||
private static final double RESIZE_COEFFICIENT = 1.5; | ||
private Object[] elements; |
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.
use the parameterized type
private Object[] elements; | |
private T [] elements; |
if (index < 0 || index >= size) { | ||
throw new ArrayListIndexOutOfBoundsException("Index outed of bounds, Index: " + index | ||
+ ", Size: " | ||
+ 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.
move this check to the private method
you use it several times
if (element == null) { | ||
for (int i = 0; i < size; i++) { | ||
if (elements[i] == null) { | ||
return remove(i); | ||
} | ||
} | ||
throw new NoSuchElementException("Element not found"); | ||
} | ||
|
||
for (int i = 0; i < size; i++) { | ||
if (element.equals(elements[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.
use the one loop
here
…and uses custom exeption