generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97ba460
commit c0e0686
Showing
1 changed file
with
70 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,109 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class ArrayList<T> implements List<T> { | ||
private static final int DEFAULT_CAPACITY = 10; | ||
private Object[] elementData; | ||
private int size; | ||
|
||
public ArrayList() { | ||
elementData = new Object[DEFAULT_CAPACITY]; | ||
} | ||
|
||
@Override | ||
public void add(T value) { | ||
|
||
if (size == elementData.length) { | ||
grow(); | ||
} | ||
elementData[size] = value; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(T value, int index) { | ||
|
||
if (index < 0 || index > size) { | ||
throw new ArrayListIndexOutOfBoundsException( | ||
"Index " + index + " out of bounds for length" + elementData.length); | ||
} | ||
if (size == elementData.length) { | ||
grow(); | ||
} | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
elementData[index] = value; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void addAll(List<T> list) { | ||
|
||
while (size + list.size() >= elementData.length) { | ||
grow(); | ||
} | ||
for (int i = 0; i < list.size(); i++) { | ||
elementData[size + i] = list.get(i); | ||
} | ||
size += list.size(); | ||
} | ||
|
||
@Override | ||
public T get(int index) { | ||
return null; | ||
checkToCorrectIndex(index); | ||
return elementData(index); | ||
} | ||
|
||
@Override | ||
public void set(T value, int index) { | ||
|
||
checkToCorrectIndex(index); | ||
elementData[index] = value; | ||
} | ||
|
||
@Override | ||
public T remove(int index) { | ||
return null; | ||
checkToCorrectIndex(index); | ||
final T removedElement = elementData(index); | ||
System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); | ||
elementData[size - 1] = null; | ||
size--; | ||
return removedElement; | ||
} | ||
|
||
@Override | ||
public T remove(T element) { | ||
return null; | ||
for (int i = 0; i < size; i++) { | ||
if (elementData[i] == element | ||
|| elementData[i] != null && elementData[i].equals(element)) { | ||
remove(i); | ||
return element; | ||
} | ||
} | ||
throw new NoSuchElementException("ArrayList don't have such element"); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return 0; | ||
return size; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
return size == 0; | ||
} | ||
|
||
private T elementData(int index) { | ||
return (T) elementData[index]; | ||
} | ||
|
||
private void checkToCorrectIndex(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new ArrayListIndexOutOfBoundsException( | ||
"Index " + index + " out of bounds for length" + elementData.length); | ||
} | ||
} | ||
|
||
private void grow() { | ||
int newCapacity = (int)(elementData.length * 1.5f); | ||
Object[] newElementData = new Object[newCapacity]; | ||
System.arraycopy(elementData, 0, newElementData, 0, elementData.length); | ||
elementData = newElementData; | ||
} | ||
} |