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
Showing
1 changed file
with
65 additions
and
8 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,105 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.NoSuchElementException; | ||
import java.util.Objects; | ||
|
||
public class ArrayList<T> implements List<T> { | ||
private static final int ARRAY_START_SIZE = 10; | ||
private static final double COEFFICIENT = 1.5; | ||
private Object[] list; | ||
private int size; | ||
|
||
public ArrayList() { | ||
list = new Object[ARRAY_START_SIZE]; | ||
} | ||
|
||
@Override | ||
public void add(T value) { | ||
|
||
growIfArrayFull(); | ||
list[size] = value; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(T value, int index) { | ||
|
||
if (index > size() || index < 0) { | ||
throw new ArrayListIndexOutOfBoundsException("Index out of range"); | ||
} | ||
if (index == size()) { | ||
add(value); | ||
return; | ||
} | ||
growIfArrayFull(); | ||
Object[] newList = new Object[list.length]; | ||
System.arraycopy(list, 0, newList, 0, index); | ||
newList[index] = value; | ||
System.arraycopy(list, index, newList, index + 1, size() - index); | ||
list = newList; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void addAll(List<T> list) { | ||
for (int i = 0; i < list.size(); i++) { | ||
add(list.get(i)); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public T get(int index) { | ||
return null; | ||
checkIndexIsValid(index); | ||
return (T) list[index]; | ||
} | ||
|
||
@Override | ||
public void set(T value, int index) { | ||
|
||
checkIndexIsValid(index); | ||
list[index] = value; | ||
} | ||
|
||
@Override | ||
public T remove(int index) { | ||
return null; | ||
checkIndexIsValid(index); | ||
final T oldElement = (T) list[index]; | ||
System.arraycopy(list, index + 1, list, index, size() - 1 - index); | ||
size--; | ||
list[size] = null; | ||
return oldElement; | ||
} | ||
|
||
@Override | ||
public T remove(T element) { | ||
return null; | ||
for (int index = 0; index < size; index++) { | ||
if (Objects.equals(element, list[index])) { | ||
return remove(index); | ||
} | ||
} | ||
throw new NoSuchElementException("No such element " + element); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return 0; | ||
return size; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
return size == 0; | ||
} | ||
|
||
private void checkIndexIsValid(int index) { | ||
if (index >= size() || index < 0) { | ||
throw new ArrayListIndexOutOfBoundsException("Index out of range"); | ||
} | ||
} | ||
|
||
private void growIfArrayFull() { | ||
if (list.length <= size) { | ||
int newSize = (int) (list.length * COEFFICIENT); | ||
Object[] temp = new Object[newSize]; | ||
System.arraycopy(list, 0, temp, 0, size); | ||
list = temp; | ||
} | ||
} | ||
} |