From 6a5508c044ad31b6e64b27048554b5ebb85d78c8 Mon Sep 17 00:00:00 2001 From: DerTyp7 Date: Fri, 1 Dec 2023 11:05:24 +0100 Subject: [PATCH] Add ArrayList --- .idea/.gitignore | 8 ++ .idea/encodings.xml | 7 + .idea/misc.xml | 14 ++ .idea/uiDesigner.xml | 124 ++++++++++++++++++ .idea/vcs.xml | 6 + pom.xml | 47 +++++++ src/main/java/de/szut/App.java | 13 ++ src/main/java/de/szut/MyArrayList.java | 55 ++++++++ src/test/java/de/szut/MyArrayListTest.java | 144 +++++++++++++++++++++ 9 files changed, 418 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/de/szut/App.java create mode 100644 src/main/java/de/szut/MyArrayList.java create mode 100644 src/test/java/de/szut/MyArrayListTest.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..df00c07 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..968594f --- /dev/null +++ b/pom.xml @@ -0,0 +1,47 @@ + + 4.0.0 + + org.example + MyArrayList + 1.0-SNAPSHOT + jar + + MyArrayList + http://maven.apache.org + + + UTF-8 + 17 + 17 + + + + + junit + junit + 3.8.1 + test + + + + org.hamcrest + hamcrest-core + 2.2 + test + + + junit + junit + 4.13.2 + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + + \ No newline at end of file diff --git a/src/main/java/de/szut/App.java b/src/main/java/de/szut/App.java new file mode 100644 index 0000000..f9bc480 --- /dev/null +++ b/src/main/java/de/szut/App.java @@ -0,0 +1,13 @@ +package de.szut; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/src/main/java/de/szut/MyArrayList.java b/src/main/java/de/szut/MyArrayList.java new file mode 100644 index 0000000..a2e8c87 --- /dev/null +++ b/src/main/java/de/szut/MyArrayList.java @@ -0,0 +1,55 @@ +package de.szut; + +import java.util.Arrays; + +public class MyArrayList { + + private static final int INITIAL_CAPACITY = 10; + + private int[] array; + private int size; + + public MyArrayList() { + this.array = new int[INITIAL_CAPACITY]; + this.size = 0; + } + + public int size() { + return size; + } + + public boolean add(int value) { + ensureCapacity(); + array[size++] = value; + return true; + } + + public void remove(int index) { + if (index < 0 || index >= size) { + throw new RuntimeException("Dieser Index existiert nicht!"); + } + + for (int i = index; i < size - 1; i++) { + array[i] = array[i + 1]; + } + + array[--size] = 0; + } + + + public boolean contains(int value) { + for (int i = 0; i < size; i++) { + if (array[i] == value) { + return true; + } + } + return false; + } + + private void ensureCapacity() { + if (size == array.length) { + int newCapacity = array.length + INITIAL_CAPACITY; + array = Arrays.copyOf(array, newCapacity); + } + } +} diff --git a/src/test/java/de/szut/MyArrayListTest.java b/src/test/java/de/szut/MyArrayListTest.java new file mode 100644 index 0000000..e4c292c --- /dev/null +++ b/src/test/java/de/szut/MyArrayListTest.java @@ -0,0 +1,144 @@ +package de.szut; + +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.fail; + +public class MyArrayListTest { + + private MyArrayList list; + + @Before + public void setUp() { + list = new MyArrayList(); + } + + + + @Test + public void add_ShouldReturnTrueAndIncreaseSize() { + assertThat(list.size(), is(0)); + + boolean added1 = list.add(5); + assertThat(added1, is(true)); + assertThat(list.size(), is(1)); + assertThat(list.contains(5), is(true)); + + boolean added2 = list.add(10); + assertThat(added2, is(true)); + assertThat(list.size(), is(2)); + assertThat(list.contains(10), is(true)); + } + + @Test + public void remove_ShouldRemoveElementAndDecreaseSize() { + list.add(5); + list.add(10); + list.add(15); + + assertThat(list.size(), is(3)); + + list.remove(1); + + assertThat(list.size(), is(2)); + assertThat(list.contains(5), is(true)); + assertThat(list.contains(10), is(false)); + assertThat(list.contains(15), is(true)); + } + + @Test + public void remove_ShouldThrowExceptionForInvalidIndex() { + list.add(5); + + assertThat(list.size(), is(1)); + + try { + list.remove(1); + fail("Expected RuntimeException was not thrown"); + } catch (RuntimeException e) { + assertThat(e.getMessage(), is("Dieser Index existiert nicht!")); + assertThat(list.size(), is(1)); + } + } + + @Test + public void size_ShouldReturnZeroInitially() { + assertThat(list.size(), is(0)); + } + + @Test + public void size_ShouldBeZeroInitially() { + + assertThat(list.size(), is(0)); + } + + @Test + public void size_ShouldIncreaseWhenElementAdded() { + list.add(5); + + assertThat(list.size(), is(1)); + } + + @Test + public void size_ShouldIncreaseProperlyWhenMultipleElementsAdded() { + list.add(5); + list.add(10); + + assertThat(list.size(), is(2)); + } + + @Test + public void size_ShouldDecreaseWhenElementRemoved() { + list.add(5); + list.add(10); + + list.remove(0); + + assertThat(list.size(), is(1)); + } + + @Test + public void size_ShouldNotDecreaseBelowZero() { + list.add(5); + + list.remove(0); + + assertThat(list.size(), is(0)); + } + + + @Test + public void contains_ShouldReturnTrueIfElementPresent() { + list.add(5); + list.add(10); + list.add(15); + + assertThat(list.contains(10), is(true)); + } + + @Test + public void contains_ShouldReturnFalseIfElementNotPresent() { + list.add(5); + list.add(10); + list.add(15); + + assertThat(list.contains(20), is(false)); + } + + @Test + public void dynamicSize_ShouldIncreaseAsElementsAdded() { + for (int i = 1; i <= 15; i++) { + boolean added = list.add(i); + assertThat(added, is(true)); + } + + assertThat(list.size(), is(15)); + + for (int i = 1; i <= 15; i++) { + assertThat(list.contains(i), is(true)); + } + } +}