Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
0b0a61e
GP-24 Implement CrazyLambdas with comments
tboychuk Dec 28, 2020
06339fa
GP-24 Implement CrazyOptionals and copied impl of CrazyStreams
tboychuk Dec 28, 2020
86eca57
Merge branch 'main' into exercise/completed
tboychuk Dec 28, 2020
fc238d7
Merge pull request #3 from bobocode-projects/main
shryhus Dec 29, 2020
8484b36
Merge branch 'main' into exercise/completed
shryhus Dec 29, 2020
0397a73
Merge branch 'main' into exercise/completed
tboychuk Jan 4, 2021
12e944f
Merge branch 'main' into exercise/completed
tboychuk Jan 5, 2021
373d7e3
Merge pull request #12 from bobocode-projects/main
tboychuk Jan 5, 2021
54f3773
Merge branch 'main' into exercise/completed
tboychuk Jan 6, 2021
50d2965
Merge branch 'main' into exercise/completed
tboychuk Jan 6, 2021
9329d5d
GP-26 add the exercise solution with a tail and extra methods
shryhus Jan 5, 2021
4bece28
Merge pull request #16 from bobocode-projects/GP-26_Add_compleated_so…
tboychuk Jan 6, 2021
cf23eca
Merge branch 'main' into exercise/completed
tboychuk Jan 6, 2021
4b74417
Merge branch 'main' into exercise/completed
tboychuk Jan 6, 2021
54bdcc2
Merge branch 'main' into exercise/completed
tboychuk Jan 6, 2021
760dbfc
GP-31 migrate java oop
shryhus Jan 5, 2021
787778d
Merge pull request #18 from bobocode-projects/GP-31_migrate_java_oop_…
tboychuk Jan 6, 2021
97be0be
Merge branch 'main' into exercise/completed
tboychuk Jan 6, 2021
49adec3
GP-28 Add File Reader solution to exercise/completed
shryhus Jan 5, 2021
b917396
GP-28 fix imports
tboychuk Jan 6, 2021
c874d38
Merge pull request #17 from bobocode-projects/GP-28_Migrate_File_Read…
tboychuk Jan 6, 2021
947b71e
GP-25 add new README.md for functional programming basics
tboychuk Jan 6, 2021
47923cb
GP-25 add Lear or Skip section
tboychuk Jan 6, 2021
10eb8ed
Merge branch 'main' into exercise/completed
tboychuk Jan 14, 2021
6232624
Merge branch 'main' into exercise/completed
shryhus Jan 14, 2021
b563231
GP-32 Update Stack(0-2 + 0-6) for completed
shryhus Jan 13, 2021
4daf746
GP-32 Fix the tdd StackTest
shryhus Jan 14, 2021
5dfdbb1
Merge pull request #25 from bobocode-projects/GP-32_Merge_stack_into_…
tboychuk Jan 14, 2021
0f65961
Merge branch 'main' into exercise/completed
tboychuk Jan 14, 2021
d55d67a
GP-33 Add ArrayList completed solution
shryhus Jan 13, 2021
5e2d4c8
Merge pull request #27 from bobocode-projects/GP-33_a_new_ArrayList_e…
tboychuk Jan 14, 2021
f19fb37
Merge branch 'GP-29-Migrate_FileStats_into_Core' into GP-29-Migrate_F…
MrMaksym Jan 16, 2021
d574ffd
GP-29: complete FileStats.java
MrMaksym Jan 16, 2021
b4e2ac5
Merge branch 'GP-29-Migrate_FileStats_into_Core' into GP-29-Migrate_F…
MrMaksym Jan 16, 2021
8c2f26d
Merge branch 'GP-29-Migrate_FileStats_into_Core' into GP-29-Migrate_F…
MrMaksym Jan 16, 2021
62dbe3e
Merge branch 'main' into exercise/completed
tboychuk Jan 18, 2021
46fb57c
Merge pull request #30 from maxstasiuk92/GP-29-Migrate_FileStats_into…
tboychuk Jan 18, 2021
8e2dfed
Merge branch 'GP-41_Fix_method_remove()_of_a_List_into_main' into GP-…
shryhus Jan 20, 2021
3a9ca66
GP-41 Update completed methods
shryhus Jan 20, 2021
127e62a
Merge branch 'GP-41_Fix_method_remove()_of_a_List_into_main' into GP-…
shryhus Jan 20, 2021
0185b13
GP-41 Update completed solution
shryhus Jan 20, 2021
513547d
Merge pull request #32 from bobocode-projects/GP-41_Fix_method_remove…
tboychuk Jan 20, 2021
6160bf6
Merge branch 'main' into exercise/completed
tboychuk Jan 20, 2021
a8cbabf
GP-38 Fix test name in tdd completed
shryhus Jan 21, 2021
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> {

private 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 = Arrays.copyOf(elementData, size * 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,8 +149,13 @@ public void set(int index, T element) {
* @return deleted element
*/
@Override
@SuppressWarnings("unchecked")
public T remove(int index) {
throw new ExerciseNotCompletedException(); // todo: implement this method
Objects.checkIndex(index, size);
T deletedElement = (T) elementData[index];
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
size--;
return deletedElement;
}

/**
Expand All @@ -124,7 +166,16 @@ public T 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 @@ -134,22 +185,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;
}
}
Loading