Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/core/basesyntax/ArrayList.java
  • Loading branch information
PiotrHic committed Jan 21, 2025
2 parents 13f8c44 + 99c3b7d commit 2c77206
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 113 deletions.
121 changes: 9 additions & 112 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,151 +1,48 @@
package core.basesyntax;

import java.util.Collection;
import java.util.NoSuchElementException;

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

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

public ArrayList(Collection<? extends T> c) {
if (c == null) {
throw new NullPointerException("Collection cannot be null");
}
elementData = c.toArray();
setSize(elementData.length);
if (size == 0) {
setElementData(new Object[DEFAULT_CAPACITY]);
}
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

public Object[] getElementData() {
return elementData;
}

public void setElementData(Object[] elementData) {
this.elementData = elementData;
}

@Override
public void add(T value) {
ensureCapacity();
elementData[size++] = value;

}

@Override
public void add(T value, int index) {
if (index > size || index < 0) {
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index);
}
ensureCapacity();
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = value;
size++;

}

@Override
public void addAll(List<T> list) {
if (list == null) {
throw new NullPointerException("List cannot be null");
}
int newSize = size + list.size();
ensureCapacity(newSize);
for (int i = 0; i < list.size(); i++) {
elementData[size + i] = list.get(i);
}
size = newSize;

}

@Override
public T get(int index) {
checkIndex(index);
return (T) elementData[index];
return null;
}

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

}

@Override
public T remove(int index) {
checkIndex(index);
T removedElement = (T) elementData[index];
int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(elementData, index + 1, elementData, index, numMoved);
}
elementData[--size] = null;
return removedElement;
return null;
}

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

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

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

private void ensureCapacity() {
if (size == elementData.length) {
growCapacity();
}
}

private void ensureCapacity(int minCapacity) {
if (minCapacity > elementData.length) {
growCapacity(minCapacity);
}
}

private void growCapacity() {
int newCapacity = elementData.length + (elementData.length >> 1);
elementData = copyOf(elementData, newCapacity);
}

private void growCapacity(int minCapacity) {
int newCapacity = Math.max(elementData.length + (elementData.length >> 1), minCapacity);
elementData = copyOf(elementData, newCapacity);
}

private void checkIndex(int index) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index);
}
}

private Object[] copyOf(Object[] original, int newLength) {
Object[] newArray = new Object[newLength];
for (int i = 0; i < original.length && i < newLength; i++) {
newArray[i] = original[i];
}
return newArray;
return false;
}
}
4 changes: 3 additions & 1 deletion src/test/java/core/basesyntax/ArrayListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void removeElementFromArrayListByIndex() {
"Java", actualResult);
Assert.assertEquals("Test failed! Size of array after removed element should be "
+ 3 + "but it is " + arrayList.size(), 3, arrayList.size());

Assert.assertEquals("Test failed! Can't remove element by index ",
"Private", arrayList.get(2));
actualResult = arrayList.remove(0);
Expand Down Expand Up @@ -231,7 +232,7 @@ public void removeObjectValueByValue() {
cats.add(thirdCat);
Assert.assertEquals("Test failed! Size of array should be " + 3 + "but it is "
+ cats.size(), 3, cats.size());
Cat actualResult = cats.remove(fourthCat);
Cat actualResult = cats.remove(fourthCat);
Assert.assertEquals("Test failed! Returned value should be " + actualResult.toString(),
fourthCat, actualResult);
Assert.assertEquals("Test failed! Size of array should be " + 2 + "but it is "
Expand Down Expand Up @@ -318,3 +319,4 @@ public void checkIsEmpty() {
Assert.assertTrue("Test failed! ArrayList should be empty", arrayList.isEmpty());
}
}

0 comments on commit 2c77206

Please sign in to comment.