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
74 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,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; | ||
|
||
} | ||
} | ||
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); | ||
} | ||
} | ||
|
||
|