Skip to content

Commit e75f2e6

Browse files
authored
Merge pull request #16675 from sk1418/insert-1st-array
[insert-1st-array] prepend an element to an array
2 parents c22bade + a7b342a commit e75f2e6

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

core-java-modules/core-java-arrays-operations-basic/src/main/java/com/baeldung/array/operations/ArrayOperations.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.baeldung.array.operations;
22

3-
import org.apache.commons.lang3.ArrayUtils;
4-
53
import java.lang.reflect.Array;
6-
import java.util.*;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
import java.util.LinkedHashSet;
7+
import java.util.LinkedList;
8+
import java.util.Random;
9+
import java.util.Set;
710
import java.util.function.Function;
811
import java.util.function.IntPredicate;
912
import java.util.function.Predicate;
1013
import java.util.stream.Stream;
1114

15+
import org.apache.commons.lang3.ArrayUtils;
16+
1217
public class ArrayOperations {
1318

1419
// Get the first and last item of an array
@@ -225,7 +230,7 @@ public static Integer[] addElementUsingPureJava(Integer[] srcArray, int elementT
225230
public static int[] insertAnElementAtAGivenIndex(final int[] srcArray, int index, int newElement) {
226231
int[] destArray = new int[srcArray.length + 1];
227232
int j = 0;
228-
for (int i = 0; i < destArray.length - 1; i++) {
233+
for (int i = 0; i < destArray.length; i++) {
229234

230235
if (i == index) {
231236
destArray[i] = newElement;
@@ -236,4 +241,11 @@ public static int[] insertAnElementAtAGivenIndex(final int[] srcArray, int index
236241
}
237242
return destArray;
238243
}
239-
}
244+
245+
public static int[] prependAnElementToArray(int[] srcArray, int element) {
246+
int[] newArray = new int[srcArray.length + 1];
247+
newArray[0] = element;
248+
System.arraycopy(srcArray, 0, newArray, 1, srcArray.length);
249+
return newArray;
250+
}
251+
}

core-java-modules/core-java-arrays-operations-basic/src/test/java/com/baeldung/array/operations/ArrayOperationsUnitTest.java

+41-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.baeldung.array.operations;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.Assert.assertArrayEquals;
5+
36
import java.util.Arrays;
7+
8+
import org.apache.commons.lang3.ArrayUtils;
49
import org.assertj.core.api.Condition;
510
import org.junit.jupiter.api.BeforeEach;
611
import org.junit.jupiter.api.Test;
7-
import static org.assertj.core.api.Assertions.assertThat;
8-
import static org.junit.Assert.assertArrayEquals;
912

1013
public class ArrayOperationsUnitTest {
1114

@@ -314,7 +317,8 @@ public void whenShufflingIntArraySeveralTimes_thenAtLeastOneWithDifferentOrder()
314317
Condition<int[]> atLeastOneArraysIsNotEqual = new Condition<int[]>("at least one output should be different (order-wise)") {
315318
@Override
316319
public boolean matches(int[] value) {
317-
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);
320+
return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) || !Arrays.equals(value, output4) ||
321+
!Arrays.equals(value, output5) || !Arrays.equals(value, output6);
318322
}
319323
};
320324

@@ -333,7 +337,8 @@ public void whenShufflingObjectArraySeveralTimes_thenAtLeastOneWithDifferentOrde
333337
Condition<Integer[]> atLeastOneArraysIsNotEqual = new Condition<Integer[]>("at least one output should be different (order-wise)") {
334338
@Override
335339
public boolean matches(Integer[] value) {
336-
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);
340+
return !Arrays.equals(value, output) || !Arrays.equals(value, output2) || !Arrays.equals(value, output3) || !Arrays.equals(value, output4) ||
341+
!Arrays.equals(value, output5) || !Arrays.equals(value, output6);
337342
}
338343
};
339344

@@ -367,14 +372,38 @@ public void givenSourceArrayAndElement_whenAddElementUsingPureJavaIsInvoked_then
367372
}
368373

369374
@Test
370-
public void whenInsertAnElementAtAGivenIndexCalled_thenShiftTheFollowingElementsAndInsertTheElementInArray() {
371-
int[] expectedArray = { 1, 4, 2, 3, 0 };
372-
int[] anArray = new int[4];
373-
anArray[0] = 1;
374-
anArray[1] = 2;
375-
anArray[2] = 3;
376-
int[] outputArray = ArrayOperations.insertAnElementAtAGivenIndex(anArray, 1, 4);
375+
void whenInsertAnElementAtAGivenIndexCalled_thenShiftTheFollowingElementsAndInsertTheElementInArray() {
376+
int[] expectedArray = { 1, 2, 42, 3, 4 };
377+
int[] anArray = { 1, 2, 3, 4 };
378+
int[] outputArray = ArrayOperations.insertAnElementAtAGivenIndex(anArray, 2, 42);
377379

378380
assertThat(outputArray).containsExactly(expectedArray);
379381
}
380-
}
382+
383+
@Test
384+
void whenPrependingAnElementUsingInsertAnElementAtAGivenIndex_thenGetExpectedResult() {
385+
int[] anArray = { 1, 2, 3, 4 };
386+
int[] expectedArray = { 42, 1, 2, 3, 4 };
387+
int[] result = ArrayOperations.insertAnElementAtAGivenIndex(anArray, 0, 42);
388+
389+
assertThat(result).containsExactly(expectedArray);
390+
}
391+
392+
@Test
393+
void whenUsingPrependAnElementToArray_thenGetExpectedResult() {
394+
int[] anArray = { 1, 2, 3, 4 };
395+
int[] expectedArray = { 42, 1, 2, 3, 4 };
396+
int[] result = ArrayOperations.prependAnElementToArray(anArray, 42);
397+
398+
assertThat(result).containsExactly(expectedArray);
399+
}
400+
401+
@Test
402+
void whenPrependingAnElementUsingArrayUtils_thenGetExpectedResult() {
403+
int[] anArray = { 1, 2, 3, 4 };
404+
int[] expectedArray = { 42, 1, 2, 3, 4 };
405+
int[] result = ArrayUtils.addFirst(anArray, 42);
406+
407+
assertThat(result).containsExactly(expectedArray);
408+
}
409+
}

0 commit comments

Comments
 (0)