-
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
finally done #1542
base: master
Are you sure you want to change the base?
finally done #1542
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,114 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Arrays; | ||
import java.util.NoSuchElementException; | ||
import java.util.Objects; | ||
|
||
public class ArrayList<T> implements List<T> { | ||
private static final int DEFAULT_CAPACITY = 10; | ||
private T[] array; | ||
private int size = 0; | ||
|
||
public ArrayList() { | ||
this.array = (T[]) new Object[DEFAULT_CAPACITY]; | ||
} | ||
|
||
@Override | ||
public void add(T value) { | ||
|
||
if (size == array.length) { | ||
capacity(); | ||
} | ||
array[size] = value; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(T value, int index) { | ||
|
||
if (index < 0 || index > size) { | ||
throw new ArrayListIndexOutOfBoundsException("Out of bound"); | ||
} | ||
if (size == array.length) { | ||
capacity(); | ||
} | ||
if (index == size) { | ||
array[size] = value; | ||
} else { | ||
System.arraycopy(array, index, array, index + 1, size - index); | ||
array[index] = value; | ||
} | ||
size++; | ||
} | ||
|
||
@Override | ||
public void addAll(List<T> list) { | ||
|
||
T[] newArray = (T[]) new Object[list.size()]; | ||
for (int i = 0; i < list.size(); i++) { | ||
newArray[i] = list.get(i); | ||
add(newArray[i]); | ||
} | ||
} | ||
|
||
@Override | ||
public T get(int index) { | ||
return null; | ||
if (index < 0 || index >= size) { | ||
throw new ArrayListIndexOutOfBoundsException(" Out of bounds"); | ||
} | ||
return array[index]; | ||
|
||
} | ||
|
||
@Override | ||
public void set(T value, int index) { | ||
|
||
if (!(index < 0 || index >= size)) { | ||
array[index] = value; | ||
} else { | ||
throw new ArrayListIndexOutOfBoundsException("No such index"); | ||
} | ||
} | ||
|
||
@Override | ||
public T remove(int index) { | ||
return null; | ||
if (index < 0 || index >= size) { | ||
throw new ArrayListIndexOutOfBoundsException("Out of bounds"); | ||
} | ||
T res = array[index]; | ||
if (index < size - 1) { | ||
System.arraycopy(array, index + 1, array, index, size - index - 1); | ||
} | ||
size--; | ||
return res; | ||
} | ||
|
||
@Override | ||
public T remove(T element) { | ||
return null; | ||
int index = -1; | ||
for (int i = 0; i < size; i++) { | ||
if (Objects.equals(array[i], element)) { | ||
index = 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. Consider adding a |
||
} | ||
} | ||
if (index != -1) { | ||
return remove(index); | ||
} else { | ||
throw new NoSuchElementException("Didn't find the element"); | ||
} | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return 0; | ||
return size; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
return size == 0; | ||
} | ||
|
||
private void capacity() { | ||
array = Arrays.copyOf(array, array.length + DEFAULT_CAPACITY / 2); | ||
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 |
||
} | ||
} | ||
|
||
|
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.
Creating a new array
newArray
is unnecessary here. You can directly add elements from thelist
to the current array using theadd
method.