Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
package com.bobocode.array_list;

import com.bobocode.linked_list.List;
import com.bobocode.util.ExerciseNotCompletedException;

import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Objects;

/**
* {@link ArrayList} is an implementation of {@link List} interface. This resizable data structure
* based on an array and is simplified version of {@link java.util.ArrayList}.
*/
public class ArrayList<T> implements List<T> {

public static final int DEFAULT_CAPACITY = 5;
private Object[] elementData;
private int size;

/**
* This constructor creates an instance of {@link ArrayList} with a specific capacity of an array inside.
*
* @param initCapacity - the initial capacity of the list
* @throws IllegalArgumentException – if the specified initial capacity is negative or 0.
*/
public ArrayList(int initCapacity) {
throw new ExerciseNotCompletedException(); // todo: implement this method
if (initCapacity > 0) {
elementData = new Object[initCapacity];
} else {
throw new IllegalArgumentException();
}
}

/**
* This constructor creates an instance of {@link ArrayList} with a default capacity of an array inside.
* A default size of inner array is 5;
*/
public ArrayList() {
throw new ExerciseNotCompletedException(); // todo: implement this method
elementData = new Object[DEFAULT_CAPACITY];
}

/**
Expand All @@ -34,7 +45,11 @@ public ArrayList() {
* @return new instance
*/
public static <T> List<T> of(T... elements) {
throw new ExerciseNotCompletedException(); // todo: implement this method
List<T> list = new ArrayList<>(elements.length);
for (T element : elements) {
list.add(element);
}
return list;
}

/**
Expand All @@ -44,7 +59,15 @@ public static <T> List<T> of(T... elements) {
*/
@Override
public void add(T element) {
throw new ExerciseNotCompletedException(); // todo: implement this method
increaseDataArrayIfFull();
elementData[size] = element;
size++;
}

private void increaseDataArrayIfFull() {
if (elementData.length <= size) {
elementData = getTrimmedArrayToSize(elementData.length * 2);
}
}

/**
Expand All @@ -55,7 +78,10 @@ public void add(T element) {
*/
@Override
public void add(int index, T element) {
throw new ExerciseNotCompletedException(); // todo: implement this method
increaseDataArrayIfFull();
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = element;
size++;
}

/**
Expand All @@ -66,8 +92,10 @@ public void add(int index, T element) {
* @return en element
*/
@Override
@SuppressWarnings("unchecked")
public T get(int index) {
throw new ExerciseNotCompletedException(); // todo: implement this method
Objects.checkIndex(index, size);
return (T) elementData[index];
}

/**
Expand All @@ -77,8 +105,12 @@ public T get(int index) {
* @throws java.util.NoSuchElementException if list is empty
*/
@Override
@SuppressWarnings("unchecked")
public T getFirst() {
throw new ExerciseNotCompletedException(); // todo: implement this method
if (isEmpty()) {
throw new NoSuchElementException();
}
return (T) elementData[0];
}

/**
Expand All @@ -88,8 +120,12 @@ public T getFirst() {
* @throws java.util.NoSuchElementException if list is empty
*/
@Override
@SuppressWarnings("unchecked")
public T getLast() {
throw new ExerciseNotCompletedException(); // todo: implement this method
if (isEmpty()) {
throw new NoSuchElementException();
}
return (T) elementData[size - 1];
}

/**
Expand All @@ -101,7 +137,8 @@ public T getLast() {
*/
@Override
public void set(int index, T element) {
throw new ExerciseNotCompletedException(); // todo: implement this method
Objects.checkIndex(index, size);
elementData[index] = element;
}

/**
Expand All @@ -112,7 +149,12 @@ public void set(int index, T element) {
*/
@Override
public void remove(int index) {
throw new ExerciseNotCompletedException(); // todo: implement this method
if (index == size - 1) {
elementData = getTrimmedArrayToSize(size - 1);
} else {
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
}
size--;
}

/**
Expand All @@ -123,7 +165,16 @@ public void remove(int index) {
*/
@Override
public boolean contains(T element) {
throw new ExerciseNotCompletedException(); // todo: implement this method
if (isEmpty()) {
return false;
} else {
for (Object elem : elementData) {
if (elem.equals(element)) {
return true;
}
}
}
return false;
}

/**
Expand All @@ -133,22 +184,28 @@ public boolean contains(T element) {
*/
@Override
public boolean isEmpty() {
throw new ExerciseNotCompletedException(); // todo: implement this method
return size == 0;
}

@SuppressWarnings("unchecked")
private T[] getTrimmedArrayToSize(int size) {
return (T[]) Arrays.copyOf(elementData, size);
}

/**
* @return amount of saved elements
*/
@Override
public int size() {
throw new ExerciseNotCompletedException(); // todo: implement this method
return size;
}

/**
* Removes all list elements
*/
@Override
public void clear() {
throw new ExerciseNotCompletedException(); // todo: implement this method
elementData = new Object[DEFAULT_CAPACITY];
size = 0;
}
}