-
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
i want to take explain #1520
base: master
Are you sure you want to change the base?
i want to take explain #1520
Changes from all commits
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,139 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Arrays; | ||
import java.util.NoSuchElementException; | ||
|
||
public class ArrayList<T> implements List<T> { | ||
private int count; | ||
public static final int MAX_CAPACITY = 10; | ||
private Object[] elements = new Object[MAX_CAPACITY]; | ||
@Override | ||
public void add(T value) { | ||
if (count >= elements.length) { | ||
elements = grow(); | ||
} | ||
elements[count] = value; | ||
count++; | ||
|
||
} | ||
|
||
@Override | ||
public void add(T value, int index) { | ||
checkIndexforAdd(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 |
||
count++; | ||
Object[] newArray = new Object[elements.length + 1]; | ||
if (count > MAX_CAPACITY) { | ||
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 condition |
||
newArray = grow(); | ||
|
||
} | ||
for (int i = 0; i < newArray.length; 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. The loop should iterate over |
||
if (i == index) { | ||
break; | ||
} | ||
newArray[i] = elements[i]; | ||
} | ||
Comment on lines
+29
to
+34
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 loop should iterate only up to |
||
newArray[index] = value; | ||
for (int i = index + 1; i < newArray.length; i++) { | ||
newArray[i] = elements[i - 1]; | ||
} | ||
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. The loop should iterate from |
||
elements = newArray; | ||
|
||
} | ||
|
||
@Override | ||
public void addAll(List<T> list) { | ||
if (count > elements.length) { | ||
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 condition |
||
elements = grow(); | ||
} | ||
for (int i = 0; i < elements.length; 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. The loop should iterate over |
||
elements[i] = list.get(i); | ||
} | ||
Comment on lines
+48
to
+50
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 |
||
|
||
} | ||
|
||
@Override | ||
public T get(int index) { | ||
return null; | ||
checkIndex(index); | ||
return (T) elements[index]; | ||
} | ||
|
||
@Override | ||
public void set(T value, int index) { | ||
|
||
checkIndexforAdd(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 |
||
elements[index] = value; | ||
} | ||
|
||
@Override | ||
public T remove(int index) { | ||
return null; | ||
checkIndex(index); | ||
T element = (T) elements[index]; | ||
Object[] newArray1 = new Object[elements.length - 1]; | ||
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. Creating a new array with |
||
for (int i = 0; i < newArray1.length; i++) { | ||
if (i == index) { | ||
break; | ||
} | ||
newArray1[i] = elements[i]; | ||
} | ||
for (int i = index; i < newArray1.length; i++) { | ||
newArray1[i] = elements[i + 1]; | ||
} | ||
count--; | ||
elements = newArray1; | ||
return element; | ||
} | ||
|
||
@Override | ||
public T remove(T element) { | ||
return null; | ||
T result = null; | ||
Object[] newArray1 = new Object[elements.length - 1]; | ||
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. Similar to the |
||
int a = 0; | ||
int thisExist = 0; | ||
for (int i = 0; i < elements.length; i++) { | ||
if (elements[i] == element || (element != null | ||
&& element.equals(elements[i]))) { | ||
a = i; | ||
result = element; | ||
thisExist++; | ||
break; | ||
} | ||
newArray1[i] = elements[i]; | ||
} | ||
if (thisExist == 0) { | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
for (int i = a; i < newArray1.length; i++) { | ||
newArray1[i] = elements[i + 1]; | ||
} | ||
count--; | ||
elements = newArray1; | ||
return result; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return 0; | ||
return count; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
return count == 0; | ||
} | ||
|
||
public Object[] grow() { | ||
return Arrays.copyOf(elements, MAX_CAPACITY + (MAX_CAPACITY >> 1)); | ||
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 |
||
} | ||
|
||
private void checkIndexforAdd(int index) { | ||
if (index < 0 || index > count) { | ||
throw new ArrayListIndexOutOfBoundsException("Incorrect index: " + index); | ||
} | ||
|
||
} | ||
|
||
private void checkIndex(int index) { | ||
if (index < 0 || index >= count) { | ||
throw new ArrayListIndexOutOfBoundsException("Incorrect index: " + index); | ||
} | ||
} | ||
} |
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.
The use of
Arrays.copyOf()
in thegrow
method violates the constraint of not using utility classes. You should implement array copying manually.