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.
https://github.com/DmytroBorysenko2004/jv-array-list
- Loading branch information
1 parent
798d9ef
commit 2f72a32
Showing
1 changed file
with
84 additions
and
15 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,117 @@ | ||
package core.basesyntax; | ||
|
||
public class ArrayList<T> implements List<T> { | ||
@Override | ||
public void add(T value) { | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
public abstract class ArrayList<T> implements List<T> { | ||
private static final int DEFAULT_CAPACITY = 10; | ||
private Object[] elements; | ||
private int size; | ||
|
||
public ArrayList() { | ||
elements = new Object[DEFAULT_CAPACITY]; | ||
size = 0; | ||
} | ||
|
||
@Override | ||
public void add(T value, int index) { | ||
private void ensureCapacity(int minCapacity) { | ||
if (minCapacity > elements.length) { | ||
int newCapacity = elements.length * 3 / 2 + 1; | ||
elements = Arrays.copyOf(elements, newCapacity); | ||
} | ||
} | ||
|
||
private void rangeCheck(int index) { | ||
if (index >= size || index < 0) { | ||
throw new ArrayListIndexOutOfBoundsException("Index: " + index + ", Size: " + size); | ||
} | ||
} | ||
|
||
@Override | ||
public void addAll(List<T> list) { | ||
public boolean add(T value) { | ||
ensureCapacity(size + 1); | ||
elements[size++] = value; | ||
return true; | ||
} | ||
|
||
public void add(T value, int index) { | ||
rangeCheck(index); | ||
ensureCapacity(size + 1); | ||
System.arraycopy(elements, index, elements, index + 1, size - index); | ||
elements[index] = value; | ||
size++; | ||
} | ||
|
||
public void addAll(List<T> list) { | ||
ensureCapacity(size + list.size()); | ||
for (T element : list) { | ||
add(element); | ||
} | ||
} | ||
|
||
@Override | ||
public T get(int index) { | ||
return null; | ||
rangeCheck(index); | ||
return (T) elements[index]; | ||
} | ||
|
||
@Override | ||
public void set(T value, int index) { | ||
|
||
rangeCheck(index); | ||
elements[index] = value; | ||
} | ||
|
||
@Override | ||
public T remove(int index) { | ||
return null; | ||
rangeCheck(index); | ||
T oldValue = (T) elements[index]; | ||
int numMoved = size - index - 1; | ||
if (numMoved > 0) { | ||
System.arraycopy(elements, index + 1, elements, index, numMoved); | ||
} | ||
elements[--size] = null; | ||
return oldValue; | ||
} | ||
|
||
@Override | ||
public T remove(T element) { | ||
return null; | ||
public boolean remove(Object element) { | ||
int index = indexOf(element); | ||
if (index >= 0) { | ||
remove(index); | ||
return true; | ||
} else { | ||
throw new NoSuchElementException("Element not found"); | ||
} | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return 0; | ||
return size; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
return size == 0; | ||
} | ||
|
||
public int indexOf(Object o) { | ||
if (o == null) { | ||
for (int i = 0; i < size; i++) { | ||
if (elements[i] == null) { | ||
return i; | ||
} | ||
} | ||
} else { | ||
for (int i = 0; i < size; i++) { | ||
if (o.equals(elements[i])) { | ||
return i; | ||
} | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
public static class ArrayListIndexOutOfBoundsException extends IndexOutOfBoundsException { | ||
public ArrayListIndexOutOfBoundsException(String s) { | ||
super(s); | ||
} | ||
} | ||
} |