Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ public Integer[] addElementUsingSystemArrayCopy(Integer[] srcArray, int elementT
return destArray;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,37 @@ 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[] 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 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,15 @@ public void givenSourceArrayAndElement_whenAddElementUsingSystemArrayCopyIsInvok
Integer[] expectedArray = {1, 2, 3, 4, 5};
assertArrayEquals(expectedArray, destArray);
}

@Test
public void givenSourceArrayAndElement_whenAddElementUsingPureJavaIsInvoked_thenNewElementMustBeAdded() {
Integer[] sourceArray = {1, 2, 3, 4};
int elementToAdd = 5;

Integer[] destArray = addElementToEndOfArray.addElementUsingPureJava(sourceArray, elementToAdd);

Integer[] expectedArray = {1, 2, 3, 4, 5};
assertArrayEquals(expectedArray, destArray);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.Arrays;

import com.baeldung.arraylist.operations.ArrayListOperations;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -362,4 +363,16 @@ public void whenGetRandomFromObjectArrayToString_thenReturnItemContainedInArray(

assertThat(defaultObjectArray).contains(output);
}

@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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.baeldung.arraylist.operations;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;

import org.assertj.core.api.Condition;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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