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

finally done #1542

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
82 changes: 74 additions & 8 deletions src/main/java/core/basesyntax/ArrayList.java
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()];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new array newArray is unnecessary here. You can directly add elements from the list to the current array using the add method.

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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a break statement here to exit the loop once the element is found, as continuing the loop is unnecessary.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The capacity method increases the array size by half of the DEFAULT_CAPACITY. Consider doubling the array size instead to ensure better performance when the list grows significantly.

}
}


Loading