Merge pull request #8004 from vatsalgosar/master

BAEL-3209 Adding Element in Java Array vs ArrayList
This commit is contained in:
Erik Pragt
2019-10-20 23:22:58 +10:00
committed by GitHub
4 changed files with 142 additions and 21 deletions

View File

@@ -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;
}
}

View File

@@ -0,0 +1,23 @@
package com.baeldung.arraylist.operations;
import java.util.ArrayList;
public class ArrayListOperations {
public static Integer getAnIntegerElement(ArrayList<Integer> anArrayList, int index) {
return anArrayList.get(index);
}
public static void modifyAnIntegerElement(ArrayList<Integer> anArrayList, int index, Integer newElement) {
anArrayList.set(index, newElement);
}
public static void appendAnIntegerElement(ArrayList<Integer> anArrayList, Integer newElement) {
anArrayList.add(newElement);
}
public static void insertAnIntegerElementAtIndex(ArrayList<Integer> anArrayList, int index, Integer newElement) {
anArrayList.add(index, newElement);
}
}

View File

@@ -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<int[]> atLeastOneArraysIsNotEqual = new Condition<int[]>(
"at least one output should be different (order-wise)") {
Condition<int[]> atLeastOneArraysIsNotEqual = new Condition<int[]>("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<Integer[]> atLeastOneArraysIsNotEqual = new Condition<Integer[]>(
"at least one output should be different (order-wise)") {
Condition<Integer[]> atLeastOneArraysIsNotEqual = new Condition<Integer[]>("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);
}
}

View File

@@ -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<Integer> 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);
}
}