BAEL-3209 - Adding elements in Java Array vs ArrayList

This commit is contained in:
vatsalgosar
2019-10-12 15:51:21 +05:30
parent fd94e168ee
commit 5fa271fe02
4 changed files with 135 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
package com.baeldung.array.operations;
public class ArrayOperations {
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

@@ -0,0 +1,32 @@
package com.baeldung.array.operations;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.assertj.core.api.Assertions.assertThat;
public class ArrayOperationsUnitTest {
@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);
}
}