From 923019269cf8200221b109eda7ceabf55e1a7c4d Mon Sep 17 00:00:00 2001 From: Pavan Shankar Koli Date: Fri, 19 Apr 2019 17:12:33 +0530 Subject: [PATCH 1/2] BAEL-2770 Added code and test cases --- .../array/AddElementToEndOfArray.java | 34 ++++++++++++++ .../array/AddElementToEndOfArrayUnitTest.java | 46 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 core-java-arrays/src/main/java/com/baeldung/array/AddElementToEndOfArray.java create mode 100644 core-java-arrays/src/test/java/com/baeldung/array/AddElementToEndOfArrayUnitTest.java diff --git a/core-java-arrays/src/main/java/com/baeldung/array/AddElementToEndOfArray.java b/core-java-arrays/src/main/java/com/baeldung/array/AddElementToEndOfArray.java new file mode 100644 index 0000000000..1dc6600600 --- /dev/null +++ b/core-java-arrays/src/main/java/com/baeldung/array/AddElementToEndOfArray.java @@ -0,0 +1,34 @@ +package com.baeldung.array; + +import java.util.ArrayList; +import java.util.Arrays; + +public class AddElementToEndOfArray { + + public Integer[] addElementUsingArraysCopyOf(Integer[] srcArray, int elementToAdd) { + Integer[] destArray = Arrays.copyOf(srcArray, srcArray.length + 1); + + destArray[destArray.length-1] = elementToAdd; + return destArray; + } + + public Integer[] addElementUsingArrayList(Integer[] srcArray, int elementToAdd) { + Integer[] destArray = new Integer[srcArray.length + 1]; + + ArrayList arrayList = new ArrayList<>(Arrays.asList(srcArray)); + arrayList.add(elementToAdd); + + return arrayList.toArray(destArray); + } + + public Integer[] addElementUsingSystemArrayCopy(Integer[] srcArray, int elementToAdd) { + Integer[] destArray = new Integer[srcArray.length + 1]; + + System.arraycopy(srcArray, 0, destArray, 0, srcArray.length); + + destArray[destArray.length-1] = elementToAdd; + + return destArray; + } + +} \ No newline at end of file diff --git a/core-java-arrays/src/test/java/com/baeldung/array/AddElementToEndOfArrayUnitTest.java b/core-java-arrays/src/test/java/com/baeldung/array/AddElementToEndOfArrayUnitTest.java new file mode 100644 index 0000000000..beeed1a313 --- /dev/null +++ b/core-java-arrays/src/test/java/com/baeldung/array/AddElementToEndOfArrayUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.array; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class AddElementToEndOfArrayUnitTest { + + AddElementToEndOfArray addElementToEndOfArray; + + @Before + public void init(){ + addElementToEndOfArray = new AddElementToEndOfArray(); + } + + @Test + public void givenSourceArrayAndElement_whenAddElementUsingArraysCopyIsInvoked_thenNewElementMustBeAdded(){ + Integer[] sourceArray = {1,2,3,4}; + int elementToAdd = 5; + + Integer[] destArray = addElementToEndOfArray.addElementUsingArraysCopyOf(sourceArray, elementToAdd); + + assertEquals(elementToAdd, destArray[destArray.length-1].intValue()); + } + + @Test + public void givenSourceArrayAndElement_whenAddElementUsingArrayListIsInvoked_thenNewElementMustBeAdded(){ + Integer[] sourceArray = {1,2,3,4}; + int elementToAdd = 5; + + Integer[] destArray = addElementToEndOfArray.addElementUsingArrayList(sourceArray, elementToAdd); + + assertEquals(elementToAdd, destArray[destArray.length-1].intValue()); + } + + @Test + public void givenSourceArrayAndElement_whenAddElementUsingSystemArrayCopyIsInvoked_thenNewElementMustBeAdded(){ + Integer[] sourceArray = {1,2,3,4}; + int elementToAdd = 5; + + Integer[] destArray = addElementToEndOfArray.addElementUsingSystemArrayCopy(sourceArray, elementToAdd); + + assertEquals(elementToAdd, destArray[destArray.length-1].intValue()); + } +} From 16553936c7aed6022bc735466237e430321f34e8 Mon Sep 17 00:00:00 2001 From: "Sadanand S. Koli" Date: Mon, 22 Apr 2019 20:48:49 +0530 Subject: [PATCH 2/2] BAEL-2770 Fixed code formatting and improved test cases --- .../array/AddElementToEndOfArray.java | 6 ++--- .../array/AddElementToEndOfArrayUnitTest.java | 25 +++++++++++-------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/core-java-arrays/src/main/java/com/baeldung/array/AddElementToEndOfArray.java b/core-java-arrays/src/main/java/com/baeldung/array/AddElementToEndOfArray.java index 1dc6600600..dcd61cdfa7 100644 --- a/core-java-arrays/src/main/java/com/baeldung/array/AddElementToEndOfArray.java +++ b/core-java-arrays/src/main/java/com/baeldung/array/AddElementToEndOfArray.java @@ -8,7 +8,7 @@ public class AddElementToEndOfArray { public Integer[] addElementUsingArraysCopyOf(Integer[] srcArray, int elementToAdd) { Integer[] destArray = Arrays.copyOf(srcArray, srcArray.length + 1); - destArray[destArray.length-1] = elementToAdd; + destArray[destArray.length - 1] = elementToAdd; return destArray; } @@ -26,9 +26,9 @@ public class AddElementToEndOfArray { System.arraycopy(srcArray, 0, destArray, 0, srcArray.length); - destArray[destArray.length-1] = elementToAdd; + destArray[destArray.length - 1] = elementToAdd; return destArray; } -} \ No newline at end of file +} diff --git a/core-java-arrays/src/test/java/com/baeldung/array/AddElementToEndOfArrayUnitTest.java b/core-java-arrays/src/test/java/com/baeldung/array/AddElementToEndOfArrayUnitTest.java index beeed1a313..f6f1f954f6 100644 --- a/core-java-arrays/src/test/java/com/baeldung/array/AddElementToEndOfArrayUnitTest.java +++ b/core-java-arrays/src/test/java/com/baeldung/array/AddElementToEndOfArrayUnitTest.java @@ -3,44 +3,47 @@ package com.baeldung.array; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; public class AddElementToEndOfArrayUnitTest { AddElementToEndOfArray addElementToEndOfArray; @Before - public void init(){ + public void init() { addElementToEndOfArray = new AddElementToEndOfArray(); } @Test - public void givenSourceArrayAndElement_whenAddElementUsingArraysCopyIsInvoked_thenNewElementMustBeAdded(){ - Integer[] sourceArray = {1,2,3,4}; + public void givenSourceArrayAndElement_whenAddElementUsingArraysCopyIsInvoked_thenNewElementMustBeAdded() { + Integer[] sourceArray = {1, 2, 3, 4}; int elementToAdd = 5; Integer[] destArray = addElementToEndOfArray.addElementUsingArraysCopyOf(sourceArray, elementToAdd); - assertEquals(elementToAdd, destArray[destArray.length-1].intValue()); + Integer[] expectedArray = {1, 2, 3, 4, 5}; + assertArrayEquals(expectedArray, destArray); } @Test - public void givenSourceArrayAndElement_whenAddElementUsingArrayListIsInvoked_thenNewElementMustBeAdded(){ - Integer[] sourceArray = {1,2,3,4}; + public void givenSourceArrayAndElement_whenAddElementUsingArrayListIsInvoked_thenNewElementMustBeAdded() { + Integer[] sourceArray = {1, 2, 3, 4}; int elementToAdd = 5; Integer[] destArray = addElementToEndOfArray.addElementUsingArrayList(sourceArray, elementToAdd); - assertEquals(elementToAdd, destArray[destArray.length-1].intValue()); + Integer[] expectedArray = {1, 2, 3, 4, 5}; + assertArrayEquals(expectedArray, destArray); } @Test - public void givenSourceArrayAndElement_whenAddElementUsingSystemArrayCopyIsInvoked_thenNewElementMustBeAdded(){ - Integer[] sourceArray = {1,2,3,4}; + public void givenSourceArrayAndElement_whenAddElementUsingSystemArrayCopyIsInvoked_thenNewElementMustBeAdded() { + Integer[] sourceArray = {1, 2, 3, 4}; int elementToAdd = 5; Integer[] destArray = addElementToEndOfArray.addElementUsingSystemArrayCopy(sourceArray, elementToAdd); - assertEquals(elementToAdd, destArray[destArray.length-1].intValue()); + Integer[] expectedArray = {1, 2, 3, 4, 5}; + assertArrayEquals(expectedArray, destArray); } }