diff --git a/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/array/operations/ArrayOperations.java b/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/array/operations/ArrayOperations.java index 94a00f7aba..4513dbf899 100644 --- a/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/array/operations/ArrayOperations.java +++ b/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/array/operations/ArrayOperations.java @@ -192,15 +192,48 @@ public class ArrayOperations { return array[new Random().nextInt(array.length)]; } - public static Integer[] intersectionSimple(final Integer[] a, final Integer[] b){ - return Stream.of(a).filter(Arrays.asList(b)::contains).toArray(Integer[]::new); + public static Integer[] intersectionSimple(final Integer[] a, final Integer[] b) { + return Stream.of(a) + .filter(Arrays.asList(b)::contains) + .toArray(Integer[]::new); } - public static Integer[] intersectionSet(final Integer[] a, final Integer[] b){ - return Stream.of(a).filter(Arrays.asList(b)::contains).distinct().toArray(Integer[]::new); + public static Integer[] intersectionSet(final Integer[] a, final Integer[] b) { + return Stream.of(a) + .filter(Arrays.asList(b)::contains) + .distinct() + .toArray(Integer[]::new); } - public static Integer[] intersectionMultiSet(final Integer[] a, final Integer[] b){ - return Stream.of(a).filter(new LinkedList<>(Arrays.asList(b))::remove).toArray(Integer[]::new); + public static Integer[] intersectionMultiSet(final Integer[] a, final Integer[] b) { + return Stream.of(a) + .filter(new LinkedList<>(Arrays.asList(b))::remove) + .toArray(Integer[]::new); + } + + public static Integer[] addElementUsingPureJava(Integer[] srcArray, int elementToAdd) { + Integer[] destArray = new Integer[srcArray.length + 1]; + + for (int i = 0; i < srcArray.length; i++) { + destArray[i] = srcArray[i]; + } + + destArray[destArray.length - 1] = elementToAdd; + return destArray; + } + + public static int[] insertAnElementAtAGivenIndex(final int[] srcArray, int index, int newElement) { + int[] destArray = new int[srcArray.length + 1]; + int j = 0; + for (int i = 0; i < destArray.length - 1; i++) { + + if (i == index) { + destArray[i] = newElement; + } else { + destArray[i] = srcArray[j]; + j++; + } + } + return destArray; } } diff --git a/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/arraylist/operations/ArrayListOperations.java b/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/arraylist/operations/ArrayListOperations.java new file mode 100644 index 0000000000..b2aed553da --- /dev/null +++ b/core-java-modules/core-java-arrays-2/src/main/java/com/baeldung/arraylist/operations/ArrayListOperations.java @@ -0,0 +1,23 @@ +package com.baeldung.arraylist.operations; + +import java.util.ArrayList; + +public class ArrayListOperations { + + public static Integer getAnIntegerElement(ArrayList anArrayList, int index) { + return anArrayList.get(index); + } + + public static void modifyAnIntegerElement(ArrayList anArrayList, int index, Integer newElement) { + anArrayList.set(index, newElement); + } + + public static void appendAnIntegerElement(ArrayList anArrayList, Integer newElement) { + anArrayList.add(newElement); + } + + public static void insertAnIntegerElementAtIndex(ArrayList anArrayList, int index, Integer newElement) { + anArrayList.add(index, newElement); + } + +} diff --git a/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/array/operations/ArrayOperationsUnitTest.java b/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/array/operations/ArrayOperationsUnitTest.java index d72681ca05..ea9bf2c5a9 100644 --- a/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/array/operations/ArrayOperationsUnitTest.java +++ b/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/array/operations/ArrayOperationsUnitTest.java @@ -1,12 +1,11 @@ package com.baeldung.array.operations; +import java.util.Arrays; import org.assertj.core.api.Condition; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - -import java.util.Arrays; - import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertArrayEquals; public class ArrayOperationsUnitTest { @@ -262,8 +261,7 @@ public class ArrayOperationsUnitTest { @Test public void whenMapIntArrayToString_thenReturnArray() { - String[] expectedArray = new String[] { "Value: 3", "Value: 5", "Value: 2", "Value: 5", "Value: 14", - "Value: 4" }; + String[] expectedArray = new String[] { "Value: 3", "Value: 5", "Value: 2", "Value: 5", "Value: 14", "Value: 4" }; String[] output = ArrayOperations.mapIntArrayToString(defaultIntArray); assertThat(output).containsExactly(expectedArray); @@ -313,13 +311,10 @@ public class ArrayOperationsUnitTest { int[] output5 = ArrayOperations.shuffleIntArray(defaultIntArray); int[] output6 = ArrayOperations.shuffleIntArray(defaultIntArray); - Condition atLeastOneArraysIsNotEqual = new Condition( - "at least one output should be different (order-wise)") { + Condition atLeastOneArraysIsNotEqual = new Condition("at least one output should be different (order-wise)") { @Override public boolean matches(int[] value) { - return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) - || !Arrays.equals(value, output4) || !Arrays.equals(value, output5) - || !Arrays.equals(value, output6); + return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) || !Arrays.equals(value, output4) || !Arrays.equals(value, output5) || !Arrays.equals(value, output6); } }; @@ -335,13 +330,10 @@ public class ArrayOperationsUnitTest { Integer[] output5 = ArrayOperations.shuffleObjectArray(defaultObjectArray); Integer[] output6 = ArrayOperations.shuffleObjectArray(defaultObjectArray); - Condition atLeastOneArraysIsNotEqual = new Condition( - "at least one output should be different (order-wise)") { + Condition atLeastOneArraysIsNotEqual = new Condition("at least one output should be different (order-wise)") { @Override public boolean matches(Integer[] value) { - return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) - || !Arrays.equals(value, output4) || !Arrays.equals(value, output5) - || !Arrays.equals(value, output6); + return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) || !Arrays.equals(value, output4) || !Arrays.equals(value, output5) || !Arrays.equals(value, output6); } }; @@ -362,4 +354,27 @@ public class ArrayOperationsUnitTest { assertThat(defaultObjectArray).contains(output); } + + @Test + public void givenSourceArrayAndElement_whenAddElementUsingPureJavaIsInvoked_thenNewElementMustBeAdded() { + Integer[] sourceArray = { 1, 2, 3, 4 }; + int elementToAdd = 5; + + Integer[] destArray = ArrayOperations.addElementUsingPureJava(sourceArray, elementToAdd); + + Integer[] expectedArray = { 1, 2, 3, 4, 5 }; + assertArrayEquals(expectedArray, destArray); + } + + @Test + public void whenInsertAnElementAtAGivenIndexCalled_thenShiftTheFollowingElementsAndInsertTheElementInArray() { + int[] expectedArray = { 1, 4, 2, 3, 0 }; + int[] anArray = new int[4]; + anArray[0] = 1; + anArray[1] = 2; + anArray[2] = 3; + int[] outputArray = ArrayOperations.insertAnElementAtAGivenIndex(anArray, 1, 4); + + assertThat(outputArray).containsExactly(expectedArray); + } } diff --git a/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/arraylist/operations/ArrayListOperationsUnitTest.java b/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/arraylist/operations/ArrayListOperationsUnitTest.java new file mode 100644 index 0000000000..1ec7645d8f --- /dev/null +++ b/core-java-modules/core-java-arrays-2/src/test/java/com/baeldung/arraylist/operations/ArrayListOperationsUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.arraylist.operations; + +import java.util.ArrayList; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; + +public class ArrayListOperationsUnitTest { + + private ArrayList anArrayList; + + @BeforeEach + public void setupDefaults() { + anArrayList = new ArrayList<>(); + anArrayList.add(2); + anArrayList.add(3); + anArrayList.add(4); + } + + @Test + public void whenGetAnIntegerElementCalled_thenReturnTheIntegerElement() { + Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, 1); + + assertThat(output).isEqualTo(3); + } + + @Test + public void whenModifyAnIntegerElementCalled_thenModifyTheIntegerElement() { + ArrayListOperations.modifyAnIntegerElement(anArrayList, 2, 5); + Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, 2); + + assertThat(output).isEqualTo(5); + } + + @Test + public void whenAppendAnIntegerElementCalled_thenTheIntegerElementIsAppendedToArrayList() { + ArrayListOperations.appendAnIntegerElement(anArrayList, 6); + Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, anArrayList.size() - 1); + + assertThat(output).isEqualTo(6); + } + + @Test + public void whenInsertAnIntegerAtIndexCalled_thenTheIntegerElementIsInseredToArrayList() { + ArrayListOperations.insertAnIntegerElementAtIndex(anArrayList, 1, 10); + Integer output = ArrayListOperations.getAnIntegerElement(anArrayList, 1); + + assertThat(output).isEqualTo(10); + } +} \ No newline at end of file