From d5330bbee725599eca7e4f2531ec1e1ea9cf4ff4 Mon Sep 17 00:00:00 2001 From: Eric Barnaba Date: Thu, 8 Mar 2018 15:16:57 -0500 Subject: [PATCH 01/11] saving --- .idea/.name | 1 + .idea/compiler.xml | 16 ++ .idea/libraries/Maven__junit_junit_4_12.xml | 13 + .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 + .idea/misc.xml | 13 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 271 ++++++++++++++++++ generics.iml | 16 ++ src/main/java/.deleteme | 0 src/test/java/.deleteme | 0 11 files changed, 357 insertions(+) create mode 100644 .idea/.name create mode 100644 .idea/compiler.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_12.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 generics.iml delete mode 100644 src/main/java/.deleteme delete mode 100644 src/test/java/.deleteme diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..1f58a0f --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +generics \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..c20d346 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 0000000..d411041 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d30d09e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ca63f44 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ 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/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..cbeea08 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,271 @@ + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -108,7 +206,7 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -190,6 +388,22 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + - + - + @@ -249,9 +497,66 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/MyArrayList.java b/src/main/java/MyArrayList.java new file mode 100644 index 0000000..692b0c5 --- /dev/null +++ b/src/main/java/MyArrayList.java @@ -0,0 +1,163 @@ + +import java.util.Arrays; + +public class MyArrayList { + + + + private T[] myArray; + + + + + public MyArrayList(){ + this.myArray = (T[]) new Object[0]; + + } + + public MyArrayList(int size){ + this.myArray = (T[]) new Object[size]; + + } + + public void add(T element){ + if(this.contains(null)){ + this.set(this.indexOf(null), element); + } + else{ + this.myArray = Arrays.copyOf(myArray, myArray.length+1); + this.myArray[this.myArray.length-1] = element; + } + } + + public void add(int index, T element){ + if(this.myArray[index] == null){ + this.set(index,element); + } + else{ + this.myArray = Arrays.copyOf(myArray, myArray.length+1); + T[] tempArray = Arrays.copyOf(myArray, myArray.length); + for(int i = index+1; i T[] toArray(T[] newArray){ + + return (T[]) Arrays.copyOf(this.myArray, this.myArray.length, newArray.getClass()); + + } + + public void trimToSize(){ + + } + + private int countNulls(){ + int counter = 0; + for (T t : this.myArray){ + if (t == null){ + counter++; + } + } + return counter; + } + + private int newArraySize(T[] input){ + if(this.countNulls() >= input.length){ + return this.myArray.length; + } + else{ + return this.myArray.length + (input.length-this.countNulls()); + } + } + + + + + + + + + +} diff --git a/src/test/java/MyArrayListTest.java b/src/test/java/MyArrayListTest.java new file mode 100644 index 0000000..6f6aa71 --- /dev/null +++ b/src/test/java/MyArrayListTest.java @@ -0,0 +1,265 @@ +import org.junit.Assert; +import org.junit.Test; + +public class MyArrayListTest { + + @Test + public void nullaryConstructorTest(){ + //Given + MyArrayList test = new MyArrayList<>(); + + //When + int expected = 0; + int actual = test.size(); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void intConstructorTest(){ + //Given + MyArrayList test = new MyArrayList<>(5); + + //When + int expected = 5; + int actual = test.size(); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void addElementTest(){ + //Given + MyArrayList test = new MyArrayList<>(0); + + //When + test.add(10); + int expected =10; + int actual = test.get(0); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void addElementIndexTest(){ + //Given + MyArrayList test = new MyArrayList<>(0); + + //When + test.add(10); + test.add(0,25); + int expected =25; + int actual = test.get(0); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void addAllTest(){ + //Given + MyArrayList test = new MyArrayList<>(0); + + + //When + Integer[] expected = {5,10,15,20}; + test.addAll(expected); + Integer [] actual = test.toArray(new Integer[0]); + + //Then + Assert.assertArrayEquals(expected,actual); + } + + @Test + public void addAllIndexTest(){ + //Given + MyArrayList test = new MyArrayList<>(0); + test.add(0); + test.add(25); + + + //When + Integer[] expected = {0,5,10,15,20,25}; + test.addAll(1,expected); + Integer [] actual = test.toArray(new Integer[0]); + + //Then + Assert.assertArrayEquals(expected,actual); + } + + @Test + public void clearTest() { + //Given + MyArrayList test = new MyArrayList<>(3); + test.add(0); + test.add(25); + + //When + test.clear(); + int expected = 0; + int actual = test.size(); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void containsTest() { + //Given + MyArrayList test = new MyArrayList<>(0); + test.add(0); + test.add(25); + + //Then + Assert.assertTrue(test.contains(25)); + } + + @Test + public void indexOfTest(){ + //Given + MyArrayList test = new MyArrayList<>(0); + test.add(0); + test.add(25); + + //When + int expected = 1; + int actual = test.indexOf(25); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isEmptyTest(){ + //Given + MyArrayList test = new MyArrayList<>(5); + + //Then + Assert.assertTrue(test.isEmpty()); + } + + @Test + public void isEmptyFalseTest(){ + //Given + MyArrayList test = new MyArrayList<>(5); + test.add(25); + + //Then + Assert.assertFalse(test.isEmpty()); + } + + @Test + public void lastIndexOfTest() { + //Given + MyArrayList test = new MyArrayList<>(0); + Integer[] testElements = {5,10,5,10,10}; + test.addAll(testElements); + + //When + int expected = 2; + int actual = test.lastIndexOf(5); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void setTest() { + //Given + MyArrayList test = new MyArrayList<>(0); + Integer[] testElements = {5, 10, 5, 10, 10}; + test.addAll(testElements); + + //When + test.set(2,15); + int expected = 15; + int actual = test.get(2); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void removeIndexTest(){ + //Given + MyArrayList test = new MyArrayList<>(0); + Integer[] testElements = {5, 10, 5, 15, 20}; + test.addAll(testElements); + + //When + test.remove(2); + Integer[] expected = {5,10,15,20}; + Integer[] actual = test.toArray(new Integer[0]); + + //Then + Assert.assertArrayEquals(expected,actual); + } + + @Test + public void removeElementTest(){ + //Given + MyArrayList test = new MyArrayList<>(0); + Integer[] testElements = {5, 10, 5, 15, 20}; + test.addAll(testElements); + + //When + Integer value = 5; + test.remove(value); + Integer[] expected = {5,10,15,20}; + Integer[] actual = test.toArray(new Integer[0]); + + //Then + Assert.assertArrayEquals(expected,actual); + } + + @Test + public void removeRangeTest(){ + //Given + MyArrayList test = new MyArrayList<>(0); + Integer[] testElements = {5, 10, 5, 15, 20}; + test.addAll(testElements); + + //When + test.removeRange(1,3); + Integer[] expected = {5,20}; + Integer[] actual = test.toArray(new Integer[0]); + + //Then + Assert.assertArrayEquals(expected,actual); + } + + @Test + public void subArrayTest(){ + //Given + MyArrayList test = new MyArrayList<>(0); + Character[] testElements = {'a','b','c','d','e','f'}; + test.addAll(testElements); + + //When + Character[] expected = {'b','c','d','e'}; + Character[] actual = test.subArray(1,4); + + //Then + Assert.assertArrayEquals(expected,actual); + } + + @Test + public void trimToSizeTest(){ + //Given + MyArrayList test = new MyArrayList<>(6); + String [] elements = {"Hi", "I", "Am", "trimming"}; + test.addAll(elements); + + //When + test.trimToSize(); + int expected = 4; + int actual = test.size(); + + //Then + Assert.assertEquals(expected,actual); + } + +} From 7f1d5d4bb96d410b89e9218cfef447e492bd328a Mon Sep 17 00:00:00 2001 From: Eric Barnaba Date: Thu, 8 Mar 2018 20:11:35 -0500 Subject: [PATCH 03/11] saving --- .idea/workspace.xml | 143 +++++++++++++++-------------- src/main/java/MyArrayList.java | 79 +++++++++++++--- src/test/java/MyArrayListTest.java | 28 +++++- 3 files changed, 163 insertions(+), 87 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c6621b7..ccb79bf 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,8 @@ - - - + + - + - - + + - - - - - - - - - - + + + + + + + + + + + @@ -52,8 +52,8 @@ - - + + @@ -76,8 +76,8 @@ - - + + @@ -206,7 +206,7 @@ - + - + - - - - - + + + + + - - - - - + + + + + @@ -418,44 +418,44 @@ - + - - + + - + - - + + + + + - - + + - - + + - - + + - - + + - + - - - - @@ -470,11 +470,11 @@ - + - + @@ -521,16 +521,16 @@ - - + + - - + + @@ -539,19 +539,20 @@ - - + + - - - - - - - - - - + + + + + + + + + + + diff --git a/src/main/java/MyArrayList.java b/src/main/java/MyArrayList.java index 692b0c5..eb3ecba 100644 --- a/src/main/java/MyArrayList.java +++ b/src/main/java/MyArrayList.java @@ -1,11 +1,13 @@ import java.util.Arrays; +@SuppressWarnings("unchecked") public class MyArrayList { private T[] myArray; + private int numberOfObjects; @@ -28,20 +30,23 @@ public void add(T element){ this.myArray = Arrays.copyOf(myArray, myArray.length+1); this.myArray[this.myArray.length-1] = element; } + this.numberOfObjects++; } public void add(int index, T element){ if(this.myArray[index] == null){ - this.set(index,element); + this.myArray[index] = element; + this.numberOfObjects++; } + + else if(this.contains(null)){ + this.myArray = Arrays.copyOf(myArray, this.myArray.length); + this.shiftValuesAndReplaceTargets(index,element); + } + else{ - this.myArray = Arrays.copyOf(myArray, myArray.length+1); - T[] tempArray = Arrays.copyOf(myArray, myArray.length); - for(int i = index+1; i T[] toArray(T[] newArray){ + public T[] toArray(T[] newArray){ - return (T[]) Arrays.copyOf(this.myArray, this.myArray.length, newArray.getClass()); + + return (T[]) Arrays.copyOf(this.myArray, this.myArray.length, newArray.getClass()); } @@ -152,6 +191,22 @@ private int newArraySize(T[] input){ } } + private void shiftValuesAndReplaceTargets(int index, T ... elements){ + int count = index; + T[] tempArray = Arrays.copyOf(myArray, myArray.length); + for(int i = (index+elements.length); i test = new MyArrayList<>(5); //When - int expected = 5; + int expected = 0; int actual = test.size(); //Then Assert.assertEquals(expected,actual); } + @Test + public void intAddNullsTest(){ + //Given + MyArrayList test = new MyArrayList<>(5); + + //When + test.add(null); + test.add(null); + test.add(null); + int expected = 3; + int actual = test.size(); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test public void addElementTest(){ //Given @@ -46,22 +63,24 @@ public void addElementTest(){ @Test public void addElementIndexTest(){ //Given - MyArrayList test = new MyArrayList<>(0); + MyArrayList test = new MyArrayList<>(3); //When test.add(10); test.add(0,25); int expected =25; int actual = test.get(0); + Integer[] expected2 = {25,10,null}; //Then Assert.assertEquals(expected,actual); + Assert.assertArrayEquals(expected2,test.toArray(new Integer[0])); } @Test public void addAllTest(){ //Given - MyArrayList test = new MyArrayList<>(0); + MyArrayList test = new MyArrayList<>(3); //When @@ -82,8 +101,9 @@ public void addAllIndexTest(){ //When + Integer[] values = {5,10,15,20}; + test.addAll(1,values); Integer[] expected = {0,5,10,15,20,25}; - test.addAll(1,expected); Integer [] actual = test.toArray(new Integer[0]); //Then From f3c2b02575cf78e502dc19126409172177e9bdab Mon Sep 17 00:00:00 2001 From: Eric Barnaba Date: Thu, 8 Mar 2018 20:34:09 -0500 Subject: [PATCH 04/11] saving --- .idea/workspace.xml | 136 ++++++++++++++--------------- src/main/java/MyArrayList.java | 16 +++- src/test/java/MyArrayListTest.java | 36 +++++++- 3 files changed, 112 insertions(+), 76 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index ccb79bf..7a66db8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -20,20 +20,16 @@ - - + + - - - - - - - - - - + + + + + + @@ -52,8 +48,8 @@ - - + + @@ -76,8 +72,8 @@ - - + + @@ -206,7 +202,7 @@ - + - + - + - + - + - - - - - + + + + + - - - - - + + + + + @@ -418,44 +414,44 @@ - + - - + + - + - + - - + + - - + + - - + + - - + + - - + + - - + + - @@ -470,8 +466,8 @@ - - + + @@ -521,16 +517,16 @@ - - + + - - + + @@ -539,20 +535,16 @@ - - + + - - - - - - - - - - + + + + + + diff --git a/src/main/java/MyArrayList.java b/src/main/java/MyArrayList.java index eb3ecba..84ec5d0 100644 --- a/src/main/java/MyArrayList.java +++ b/src/main/java/MyArrayList.java @@ -90,6 +90,10 @@ else if(this.contains(null)){ } public void clear(){ + for(T t : this.myArray){ + t=null; + } + this.numberOfObjects=0; } @@ -134,11 +138,17 @@ public int indexOf(T element){ } public boolean isEmpty(){ - return false; + + return this.myArray.length == 0; } public int lastIndexOf(T element){ - return 0; + for(int i = this.myArray.length-1; i>=0; i--){ + if (this.myArray[i].equals(element)){ + return i; + } + } + return -1; } public void remove(int index){ @@ -149,7 +159,7 @@ public void remove(T object){ } - public void removeRange(int startInded, int endIndex){ + public void removeRange(int startIndex, int endIndex){ } diff --git a/src/test/java/MyArrayListTest.java b/src/test/java/MyArrayListTest.java index 95d44a0..21f122d 100644 --- a/src/test/java/MyArrayListTest.java +++ b/src/test/java/MyArrayListTest.java @@ -92,6 +92,21 @@ public void addAllTest(){ Assert.assertArrayEquals(expected,actual); } + @Test + public void addAllWithNullsTest(){ + //Given + MyArrayList test = new MyArrayList<>(6); + + + //When + Integer[] expected = {5,10,15,20,null,null}; + test.addAll(expected); + Integer [] actual = test.toArray(new Integer[0]); + + //Then + Assert.assertArrayEquals(expected,actual); + } + @Test public void addAllIndexTest(){ //Given @@ -110,6 +125,25 @@ public void addAllIndexTest(){ Assert.assertArrayEquals(expected,actual); } + @Test + public void addAllIndexWithNullsTest(){ + //Given + MyArrayList test = new MyArrayList<>(8); + test.add(0); + test.add(25); + + + + //When + Integer[] values = {5,10,15,20}; + test.addAll(1,values); + Integer[] expected = {0,5,10,15,20,25,null,null}; + Integer [] actual = test.toArray(new Integer[0]); + + //Then + Assert.assertArrayEquals(expected,actual); + } + @Test public void clearTest() { //Given @@ -158,7 +192,7 @@ public void isEmptyTest(){ MyArrayList test = new MyArrayList<>(5); //Then - Assert.assertTrue(test.isEmpty()); + Assert.assertFalse(test.isEmpty()); } @Test From 4e83da65c35f99942aff11e2b55d9493b219f82b Mon Sep 17 00:00:00 2001 From: Eric Barnaba Date: Fri, 9 Mar 2018 10:34:39 -0500 Subject: [PATCH 05/11] saving --- .idea/workspace.xml | 158 +++++++++++++++++++++++++++++--------------- 1 file changed, 104 insertions(+), 54 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 7a66db8..f07f693 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,7 @@ - - +