-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement ArrayList logic #1790
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,101 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import java.util.Objects; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The checklist explicitly states 'Don't use Objects, Arrays, or any other util class.' Remove this import and replace |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before the class declaration. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
| public class ArrayList<T> implements List<T> { | ||
|
Comment on lines
+4
to
5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before class implementation - violates checklist: 'Don't begin class or method implementation with an empty line.' |
||
| private final static int MIN_CAPACITY = 10; | ||
| private int currentCapacity; | ||
| private Object[] data; | ||
| private int size; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before the constructor. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
| public ArrayList() { | ||
|
Comment on lines
+10
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before constructor - violates checklist rule about empty lines before implementations. |
||
| this.data = new Object[MIN_CAPACITY]; | ||
| currentCapacity = MIN_CAPACITY; | ||
| this.size = 0; | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
| @Override | ||
| public void add(T value) { | ||
|
Comment on lines
+16
to
18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before method - violates checklist rule. |
||
|
|
||
| grow(); | ||
| data[size] = value; | ||
| size++; | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
| @Override | ||
| public void add(T value, int index) { | ||
|
Comment on lines
23
to
25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before method - violates checklist rule. |
||
|
|
||
| if (index == size) { | ||
| add(value); | ||
| return; | ||
| } | ||
| checkIndex(index); | ||
| grow(); | ||
| System.arraycopy(data, index, data, index + 1, size + 1 - index); | ||
| data[index] = value; | ||
| size++; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
|
|
||
| @Override | ||
| public void addAll(List<T> list) { | ||
|
Comment on lines
36
to
38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before method - violates checklist rule. |
||
|
|
||
| for (int i = 0; i < list.size(); i++) { | ||
| add(list.get(i)); | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
|
|
||
| @Override | ||
| public T get(int index) { | ||
|
Comment on lines
43
to
45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before method - violates checklist rule. |
||
| return null; | ||
| checkIndex(index); | ||
| return (T) data[index]; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
|
|
||
| @Override | ||
| public void set(T value, int index) { | ||
|
Comment on lines
49
to
51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before method - violates checklist rule. |
||
|
|
||
| checkIndex(index); | ||
| data[index] = value; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
|
|
||
| @Override | ||
| public T remove(int index) { | ||
|
Comment on lines
55
to
57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before method - violates checklist rule. |
||
| return null; | ||
| T value = get(index); | ||
| System.arraycopy(data, index + 1, data, index, size - index - 1); | ||
| data[--size] = null; | ||
| return value; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
|
|
||
| @Override | ||
| public T remove(T element) { | ||
|
Comment on lines
63
to
65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before method - violates checklist rule. |
||
| return null; | ||
| int index = -1; | ||
| for (int i = 0; i < size; i++) { | ||
| if (Objects.equals(data[i], element)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace with manual null-safe comparison instead of |
||
| index = i; | ||
| break; | ||
| } | ||
| } | ||
| return index == -1 ? null : remove(index); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method returns |
||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
|
|
||
| @Override | ||
| public int size() { | ||
|
Comment on lines
78
to
80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before method - violates checklist rule. |
||
| return 0; | ||
| return size; | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
| @Override | ||
| public boolean isEmpty() { | ||
|
Comment on lines
83
to
85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before method - violates checklist rule. |
||
| return false; | ||
| return size == 0; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
|
|
||
| private void grow() { | ||
|
Comment on lines
+88
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before private method - violates checklist rule. |
||
| if (size + 1 > currentCapacity) { | ||
| int newCapacity = currentCapacity + (currentCapacity >> 1); | ||
| Object[] newData = new Object[newCapacity]; | ||
| System.arraycopy(data, 0, newData, 0, data.length); | ||
| currentCapacity = newCapacity; | ||
| data = newData; | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
|
|
||
| private void checkIndex(int index) { | ||
|
Comment on lines
+98
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line before private method - violates checklist rule. |
||
| if (index < 0 || index > size - 1) { | ||
| throw new ArrayListIndexOutOfBoundsException("Index: " + index + ", size: " + size); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the empty line before this method. The checklist states 'Don't begin class or method implementation with an empty line.' |
||
| } | ||
| } | ||
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 imports
java.util.Objectswhich is explicitly prohibited by the checklist: 'Don't use Objects, Arrays, or any other util class.'