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

task jv-array-list is done #1549

Closed
Closed
Changes from 1 commit
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
81 changes: 75 additions & 6 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,62 @@
package core.basesyntax;

import java.util.Arrays;
serhii-slobodianiuk marked this conversation as resolved.
Show resolved Hide resolved
import java.util.NoSuchElementException;

public class ArrayList<T> implements List<T> {
private static final int DEFAULT_CAPACITY = 2;
private int size;
private Object[] elementData;

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

public void rangeCheck() {
if (size == elementData.length) {
grow();
}
}

public void indexCheck(int index) {
if (index >= size || index < 0) {
throw new ArrayListIndexOutOfBoundsException("Invalid index");
}
}

private Object[] grow() {
int newCapacity = elementData.length * 3 / 2 + 1;
elementData = Arrays.copyOf(elementData, newCapacity);
return elementData;
}

public void checkIfElementExists(T element) {
boolean elementFaunded = false;
serhii-slobodianiuk marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < size; i++) {
if ((element == null && elementData[i] == null)
|| (element != null && element.equals(elementData[i]))) {
elementFaunded = true;
break;
}
}
if (!elementFaunded) {
throw new NoSuchElementException("Element not found in the array");
}
}

@Override
public void add(T value) {

rangeCheck();
elementData[size] = value;
size++;
}

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

if (index > size || index < 0) {
serhii-slobodianiuk marked this conversation as resolved.
Show resolved Hide resolved
throw new ArrayListIndexOutOfBoundsException("Index out of bound ... )");
}
rangeCheck();
}

@Override
Expand All @@ -18,31 +66,52 @@ public void addAll(List<T> list) {

@Override
public T get(int index) {
return null;
indexCheck(index);
checkIfElementExistsByIndex(index);
serhii-slobodianiuk marked this conversation as resolved.
Show resolved Hide resolved
return (T) elementData[index];
}

private void checkIfElementExistsByIndex(int index) {
if (elementData[index] == null) {
throw new RuntimeException("There is no element by this index. Please call to admin to support");
}
}


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

serhii-slobodianiuk marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public T remove(int index) {
return null;
indexCheck(index);
final T removedElement = (T) elementData[index];
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
elementData[size - 1] = null;
size--;
return removedElement;
}

@Override
public T remove(T element) {
checkIfElementExists(element);
serhii-slobodianiuk marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < size; i++) {
if ((element == null && elementData[i] == null) || (element != null && element.equals(elementData[i]))) {
T removedElement = remove(i);
return removedElement;
}
}
return null;
}

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

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