Skip to content
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

completed jv-array-list #1208

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 68 additions & 9 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,107 @@
package core.basesyntax;

import java.util.NoSuchElementException;

public class ArrayList<T> implements List<T> {
private static final int DEFAULT_CAPACITY = 10;
private T[] elements;
private int size;

public ArrayList() {
elements = (T[]) new Object[DEFAULT_CAPACITY];
}

@Override
public void add(T value) {

if (size == elements.length) {
growArray();
}
elements[size++] = value;
}

@Override
public void add(T value, int index) {

if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException("Index is out of bounds");
}
if (size == elements.length) {
growArray();
}
for (int i = size - 1; i >= index; i--) {
elements[i + 1] = elements[i];
}
elements[index] = value;
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;
checkIndex(index);
return (T) elements[index];
}

@Override
public void set(T value, int index) {

checkIndex(index);
elements[index] = value;
}

@Override
public T remove(int index) {
return null;
checkIndex(index);
T removedElement = (T) elements[index];
for (int i = index; i < size - 1; i++) {
elements[i] = elements[i + 1];
}
size--;
return removedElement;
}

@Override
public T remove(T element) {
return null;
int index = indexOf(element);

if (index == -1) {
throw new NoSuchElementException("Element not found");
}
return remove(index);
}

@Override
public int size() {
return 0;
return size;
}

@Override
public boolean isEmpty() {
return false;
return size == 0;
}

private void growArray() {
T[] newArray = (T[]) new Object[elements.length * 3 / 2];
System.arraycopy(elements, 0, newArray, 0, size);
elements = newArray;
}

private int indexOf(T element) {
for (int i = 0; i < size; i++) {
if (elements[i] == element || elements[i] != null && elements[i].equals(element)) {
return i;
}
}
return -1;
}

private void checkIndex(int index) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Index is out of bounds: " + index);
}
}
}
Loading