[JAVA-616] core-java-arrays-operations-basic
* Creation * Moved code from https://www.baeldung.com/java-initialize-array * Moved code from https://www.baeldung.com/java-common-array-operations * Moved code from https://www.baeldung.com/java-add-element-to-array-vs-list * Moved code from https://www.baeldung.com/java-array-contains-value * Moved code from https://www.baeldung.com/java-array-remove-element * Moved code from https://www.baeldung.com/java-array-remove-first-element * Moved code from https://www.baeldung.com/java-array-add-element-at-the-end
This commit is contained in:
@@ -4,10 +4,5 @@ This module contains articles about Java arrays
|
||||
|
||||
## Relevant Articles
|
||||
|
||||
- [Extending an Array’s Length](https://www.baeldung.com/java-array-add-element-at-the-end)
|
||||
- [Array Operations in Java](https://www.baeldung.com/java-common-array-operations)
|
||||
- [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection)
|
||||
- [Removing an Element from an Array in Java](https://www.baeldung.com/java-array-remove-element)
|
||||
- [Removing the First Element of an Array](https://www.baeldung.com/java-array-remove-first-element)
|
||||
- [Adding an Element to a Java Array vs an ArrayList](https://www.baeldung.com/java-add-element-to-array-vs-list)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-arrays)
|
||||
|
||||
-34
@@ -1,34 +0,0 @@
|
||||
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<Integer> 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;
|
||||
}
|
||||
|
||||
}
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
public class RemoveElementFromAnArray {
|
||||
|
||||
public int[] removeAnElementWithAGivenIndex(int[] array, int index) {
|
||||
return ArrayUtils.remove(array, index);
|
||||
}
|
||||
|
||||
public int[] removeAllElementsWithGivenIndices(int[] array, int... indicies) {
|
||||
return ArrayUtils.removeAll(array, indicies);
|
||||
}
|
||||
|
||||
public int[] removeFirstOccurrenceOfGivenElement(int[] array, int element) {
|
||||
return ArrayUtils.removeElement(array, element);
|
||||
}
|
||||
|
||||
public int[] removeAllGivenElements(int[] array, int... elements) {
|
||||
return ArrayUtils.removeElements(array, elements);
|
||||
}
|
||||
|
||||
public int[] removeAllOccurrencesOfAGivenElement(int[] array, int element) {
|
||||
return ArrayUtils.removeAllOccurences(array, element);
|
||||
}
|
||||
|
||||
}
|
||||
-239
@@ -1,239 +0,0 @@
|
||||
package com.baeldung.array.operations;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.IntPredicate;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ArrayOperations {
|
||||
|
||||
// Get the first and last item of an array
|
||||
public static <T> T getFirstObject(T[] array) {
|
||||
return array[0];
|
||||
}
|
||||
|
||||
public static int getFirstInt(int[] array) {
|
||||
return array[0];
|
||||
}
|
||||
|
||||
public static <T> T getLastObject(T[] array) {
|
||||
return array[array.length - 1];
|
||||
}
|
||||
|
||||
public static int getLastInt(int[] array) {
|
||||
return array[array.length - 1];
|
||||
}
|
||||
|
||||
// Append a new item to an array
|
||||
public static <T> T[] appendObject(T[] array, T newItem) {
|
||||
return ArrayUtils.add(array, newItem);
|
||||
}
|
||||
|
||||
public static int[] appendInt(int[] array, int newItem) {
|
||||
int[] newArray = Arrays.copyOf(array, array.length + 1);
|
||||
newArray[newArray.length - 1] = newItem;
|
||||
return newArray;
|
||||
}
|
||||
|
||||
public static int[] appendIntWithUtils(int[] array, int newItem) {
|
||||
return ArrayUtils.add(array, newItem);
|
||||
}
|
||||
|
||||
// Compare two arrays to check if they have the same elements
|
||||
public static <T> boolean compareObjectArrays(T[] array1, T[] array2) {
|
||||
return Arrays.equals(array1, array2);
|
||||
}
|
||||
|
||||
public static boolean compareIntArrays(int[] array1, int[] array2) {
|
||||
return Arrays.equals(array1, array2);
|
||||
}
|
||||
|
||||
// Deep Compare (for nested arrays)
|
||||
public static <T> boolean deepCompareObjectArrayUsingArrays(T[][] array1, T[][] array2) {
|
||||
// We can use Objects.deepEquals for a broader approach
|
||||
return Arrays.deepEquals(array1, array2);
|
||||
}
|
||||
|
||||
public static boolean deepCompareIntArrayUsingArrays(int[][] array1, int[][] array2) {
|
||||
return Arrays.deepEquals(array1, array2);
|
||||
}
|
||||
|
||||
// Check if array is empty
|
||||
public static <T> boolean isEmptyObjectArrayUsingUtils(T[] array1) {
|
||||
return ArrayUtils.isEmpty(array1);
|
||||
}
|
||||
|
||||
public static boolean isEmptyIntArrayUsingUtils(int[] array1) {
|
||||
return ArrayUtils.isEmpty(array1);
|
||||
}
|
||||
|
||||
// Remove duplicates
|
||||
public static Integer[] removeDuplicateObjects(Integer[] array) {
|
||||
// We can use other ways of converting the array to a set too
|
||||
Set<Integer> set = new HashSet<>(Arrays.asList(array));
|
||||
return set.toArray(new Integer[set.size()]);
|
||||
}
|
||||
|
||||
public static int[] removeDuplicateInts(int[] array) {
|
||||
// Box
|
||||
Integer[] list = ArrayUtils.toObject(array);
|
||||
// Remove duplicates
|
||||
Set<Integer> set = new HashSet<Integer>(Arrays.asList(list));
|
||||
// Create array and unbox
|
||||
return ArrayUtils.toPrimitive(set.toArray(new Integer[set.size()]));
|
||||
}
|
||||
|
||||
// Remove duplicates preserving the order
|
||||
public static Integer[] removeDuplicateWithOrderObjectArray(Integer[] array) {
|
||||
// We can use other ways of converting the array to a set too
|
||||
Set<Integer> set = new LinkedHashSet<>(Arrays.asList(array));
|
||||
return set.toArray(new Integer[set.size()]);
|
||||
}
|
||||
|
||||
public static int[] removeDuplicateWithOrderIntArray(int[] array) {
|
||||
// Box
|
||||
Integer[] list = ArrayUtils.toObject(array);
|
||||
// Remove duplicates
|
||||
Set<Integer> set = new LinkedHashSet<Integer>(Arrays.asList(list));
|
||||
// Create array and unbox
|
||||
return ArrayUtils.toPrimitive(set.toArray(new Integer[set.size()]));
|
||||
}
|
||||
|
||||
// Print an array
|
||||
public static String printObjectArray(Integer[] array) {
|
||||
return ArrayUtils.toString(array);
|
||||
}
|
||||
|
||||
public static String printObjectArray(Integer[][] array) {
|
||||
return ArrayUtils.toString(array);
|
||||
}
|
||||
|
||||
public static String printIntArray(int[] array) {
|
||||
return ArrayUtils.toString(array);
|
||||
}
|
||||
|
||||
public static String printIntArray(int[][] array) {
|
||||
return ArrayUtils.toString(array);
|
||||
}
|
||||
|
||||
// Box or Unbox values
|
||||
public static int[] unboxIntegerArray(Integer[] array) {
|
||||
return ArrayUtils.toPrimitive(array);
|
||||
}
|
||||
|
||||
public static Integer[] boxIntArray(int[] array) {
|
||||
return ArrayUtils.toObject(array);
|
||||
}
|
||||
|
||||
// Map array values
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T, U> U[] mapObjectArray(T[] array, Function<T, U> function, Class<U> targetClazz) {
|
||||
U[] newArray = (U[]) Array.newInstance(targetClazz, array.length);
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
newArray[i] = function.apply(array[i]);
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
|
||||
public static String[] mapIntArrayToString(int[] array) {
|
||||
return Arrays.stream(array)
|
||||
.mapToObj(value -> String.format("Value: %s", value))
|
||||
.toArray(String[]::new);
|
||||
}
|
||||
|
||||
// Filter array values
|
||||
public static Integer[] filterObjectArray(Integer[] array, Predicate<Integer> predicate) {
|
||||
return Arrays.stream(array)
|
||||
.filter(predicate)
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
|
||||
public static int[] filterIntArray(int[] array, IntPredicate predicate) {
|
||||
return Arrays.stream(array)
|
||||
.filter(predicate)
|
||||
.toArray();
|
||||
}
|
||||
|
||||
// Insert item between others
|
||||
public static int[] insertBetweenIntArray(int[] array, int... values) {
|
||||
return ArrayUtils.insert(2, array, values);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T[] insertBetweenObjectArray(T[] array, T... values) {
|
||||
return ArrayUtils.insert(2, array, values);
|
||||
}
|
||||
|
||||
// Shuffling arrays
|
||||
public static int[] shuffleIntArray(int[] array) {
|
||||
// we create a different array instance for testing purposes
|
||||
int[] shuffled = Arrays.copyOf(array, array.length);
|
||||
ArrayUtils.shuffle(shuffled);
|
||||
return shuffled;
|
||||
}
|
||||
|
||||
public static <T> T[] shuffleObjectArray(T[] array) {
|
||||
// we create a different array instance for testing purposes
|
||||
T[] shuffled = Arrays.copyOf(array, array.length);
|
||||
ArrayUtils.shuffle(shuffled);
|
||||
return shuffled;
|
||||
}
|
||||
|
||||
// Get random number
|
||||
public static int getRandomFromIntArray(int[] array) {
|
||||
return array[new Random().nextInt(array.length)];
|
||||
}
|
||||
|
||||
public static <T> T getRandomFromObjectArray(T[] array) {
|
||||
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[] 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[] 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;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.array.operations;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ArraysIntersectionOperations {
|
||||
|
||||
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[] intersectionMultiSet(final Integer[] a, final Integer[] b) {
|
||||
return Stream.of(a)
|
||||
.filter(new LinkedList<>(Arrays.asList(b))::remove)
|
||||
.toArray(Integer[]::new);
|
||||
}
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
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);
|
||||
|
||||
Integer[] expectedArray = {1, 2, 3, 4, 5};
|
||||
assertArrayEquals(expectedArray, destArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSourceArrayAndElement_whenAddElementUsingArrayListIsInvoked_thenNewElementMustBeAdded() {
|
||||
Integer[] sourceArray = {1, 2, 3, 4};
|
||||
int elementToAdd = 5;
|
||||
|
||||
Integer[] destArray = addElementToEndOfArray.addElementUsingArrayList(sourceArray, elementToAdd);
|
||||
|
||||
Integer[] expectedArray = {1, 2, 3, 4, 5};
|
||||
assertArrayEquals(expectedArray, destArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSourceArrayAndElement_whenAddElementUsingSystemArrayCopyIsInvoked_thenNewElementMustBeAdded() {
|
||||
Integer[] sourceArray = {1, 2, 3, 4};
|
||||
int elementToAdd = 5;
|
||||
|
||||
Integer[] destArray = addElementToEndOfArray.addElementUsingSystemArrayCopy(sourceArray, elementToAdd);
|
||||
|
||||
Integer[] expectedArray = {1, 2, 3, 4, 5};
|
||||
assertArrayEquals(expectedArray, destArray);
|
||||
}
|
||||
}
|
||||
-82
@@ -1,82 +0,0 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class RemoveElementFromAnArrayUnitTest {
|
||||
|
||||
private final RemoveElementFromAnArray sut = new RemoveElementFromAnArray();
|
||||
private final int[] inputArray = new int[] { 40, 10, 20, 30, 40, 50 };
|
||||
|
||||
@Test
|
||||
void testRemoveAnElementWithAGivenIndex() {
|
||||
int index = 2;
|
||||
int[] modifiedArray = sut.removeAnElementWithAGivenIndex(inputArray, index);
|
||||
|
||||
assertFalse(ArrayUtils.contains(modifiedArray, inputArray[index]));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveAllElementsWithGivenIndices() {
|
||||
int first = 0;
|
||||
int last = inputArray.length - 1;
|
||||
int[] modifiedArray = sut.removeAllElementsWithGivenIndices(inputArray, first, last);
|
||||
|
||||
assertFalse(ArrayUtils.contains(modifiedArray, inputArray[first]) && ArrayUtils.contains(modifiedArray, inputArray[last]));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveElement_WhenArrayIsNull_ThrowsIndexOutOfBoundEx() {
|
||||
int index = 2;
|
||||
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> {
|
||||
sut.removeAnElementWithAGivenIndex(null, index);
|
||||
});
|
||||
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> {
|
||||
sut.removeAllElementsWithGivenIndices(null, index);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveFirstOccurrenceOfGivenElement() {
|
||||
int element = 40;
|
||||
int[] modifiedArray = sut.removeFirstOccurrenceOfGivenElement(inputArray, element);
|
||||
|
||||
int indexInInputArray = ArrayUtils.indexOf(inputArray, element);
|
||||
int indexInModifiedArray = ArrayUtils.indexOf(modifiedArray, element);
|
||||
assertFalse(indexInInputArray == indexInModifiedArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveAllGivenElements() {
|
||||
int duplicateElement = 40;
|
||||
int[] elements = new int[] { duplicateElement, 10, 50 };
|
||||
int[] modifiedArray = sut.removeAllGivenElements(inputArray, elements);
|
||||
|
||||
assertTrue(ArrayUtils.contains(modifiedArray, duplicateElement));
|
||||
assertFalse(ArrayUtils.contains(modifiedArray, elements[1]));
|
||||
assertFalse(ArrayUtils.contains(modifiedArray, elements[2]));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveAllOccurrencesOfAGivenElement() {
|
||||
int element = 40;
|
||||
int[] modifiedArray = sut.removeAllOccurrencesOfAGivenElement(inputArray, element);
|
||||
|
||||
assertFalse(ArrayUtils.contains(modifiedArray, element));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveElement_WhenArrayIsNull_ReturnsNull() {
|
||||
int element = 20;
|
||||
|
||||
assertEquals(null, sut.removeFirstOccurrenceOfGivenElement(null, element));
|
||||
assertEquals(null, sut.removeAllGivenElements(null, element));
|
||||
assertEquals(null, sut.removeAllOccurrencesOfAGivenElement(null, element));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-380
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -2,7 +2,7 @@ package com.baeldung.array.operations;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.baeldung.array.operations.ArrayOperations.*;
|
||||
import static com.baeldung.array.operations.ArraysIntersectionOperations.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class IntersectionUnitTest {
|
||||
|
||||
-62
@@ -1,62 +0,0 @@
|
||||
package com.baeldung.array.removefirst;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RemoveFirstElementUnitTest {
|
||||
|
||||
private List<String> list = new ArrayList<>();
|
||||
private LinkedList<String> linkedList = new LinkedList<>();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
|
||||
list.add("cat");
|
||||
list.add("dog");
|
||||
list.add("pig");
|
||||
list.add("cow");
|
||||
list.add("goat");
|
||||
|
||||
linkedList.add("cat");
|
||||
linkedList.add("dog");
|
||||
linkedList.add("pig");
|
||||
linkedList.add("cow");
|
||||
linkedList.add("goat");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenRemoveFirst_thenRemoved() {
|
||||
list.remove(0);
|
||||
|
||||
assertThat(list, hasSize(4));
|
||||
assertThat(list, not(contains("cat")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLinkedList_whenRemoveFirst_thenRemoved() {
|
||||
|
||||
linkedList.removeFirst();
|
||||
|
||||
assertThat(linkedList, hasSize(4));
|
||||
assertThat(linkedList, not(contains("cat")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringArray_whenRemovingFirstElement_thenArrayIsSmallerAndElementRemoved() {
|
||||
String[] stringArray = {"foo", "bar", "baz"};
|
||||
|
||||
String[] modifiedArray = Arrays.copyOfRange(stringArray, 1, stringArray.length);
|
||||
|
||||
assertThat(modifiedArray.length, is(2));
|
||||
assertThat(modifiedArray[0], is("bar"));
|
||||
}
|
||||
|
||||
}
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user