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

Overridden ArrayList methods #1211

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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: 69 additions & 8 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,109 @@
package core.basesyntax;

import java.util.NoSuchElementException;

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

public ArrayList() {
this.elementData = (T[]) new Object[DEFAULT_CAPACITY];
this.size = 0;
Copy link

Choose a reason for hiding this comment

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

You dont need to set size=0, because java makes the same instead of you.

Suggested change
this.size = 0;

}

public void indexValidator(int index) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Index is not valid");
}
}
Copy link

Choose a reason for hiding this comment

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

this method is not expected to be used outside this class, so make private.
don't forget to move it at the end of class, because of private.

Suggested change
public void indexValidator(int index) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Index is not valid");
}
}
private void indexValidator(int index) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Index is not valid");
}
}


public Object[] grow(T[] elementData, int size) {
Copy link

Choose a reason for hiding this comment

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

Method grow should be void, because it changes the main array, e.g. this.elementData.
So, you can directly access this this.elementData.

Suggested change
public Object[] grow(T[] elementData, int size) {
public void grow(T[] elementData, int size) {

if ((size + 1) >= elementData.length) {
int newCapacity = elementData.length + (elementData.length >> 1);
T[] newElementData = (T[]) new Object[newCapacity];
System.arraycopy(elementData, 0, newElementData, 0, size);
elementData = newElementData;
}
return elementData;
}
Copy link

Choose a reason for hiding this comment

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

Think about proper access modifier for grow method.


@Override
public void add(T value) {

elementData = (T[]) grow(elementData, size);
elementData[size] = value;
size++;
}

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

if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException("Index is not valid");
}
Copy link

Choose a reason for hiding this comment

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

Potentially repeating code should be extracted into private method.

Suggested change
if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException("Index is not valid");
}
if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException("Index is not valid");
}

if (size == elementData.length) {
elementData = (T[]) grow(elementData, size);
}
for (int i = size; i > index; i--) {
elementData[i] = elementData[i - 1];
}

Choose a reason for hiding this comment

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

image

elementData[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;
indexValidator(index);
return elementData[index];
}

@Override
public void set(T value, int index) {
indexValidator(index);
elementData[index] = value;

}

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

@Override
public T remove(T element) {
return null;
if (element == null) {
for (int i = 0; i < size; i++) {
if (elementData[i] == null) {
return remove(i);
}
}
} else {
for (int i = 0; i < size; i++) {
if (element.equals(elementData[i])) {
return remove(i);
}
}
}
throw new NoSuchElementException("Element not found");

Choose a reason for hiding this comment

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

can be more simple

Suggested change
if (element == null) {
for (int i = 0; i < size; i++) {
if (elementData[i] == null) {
return remove(i);
}
}
} else {
for (int i = 0; i < size; i++) {
if (element.equals(elementData[i])) {
return remove(i);
}
}
}
throw new NoSuchElementException("Element not found");
for (int i = 0; i < size; i++) {
if (element == null ? elementData[i] == null : element.equals(elementData[i])) {
return remove(i);
}
}
throw new NoSuchElementException("Element not found");

}

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

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