Skip to content

Commit 9b2148d

Browse files
authored
Merge pull request eugenp#6760 from pkoli/master
BAEL-2770 Added code and test cases
2 parents b1aaff5 + 1655393 commit 9b2148d

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.array;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
public class AddElementToEndOfArray {
7+
8+
public Integer[] addElementUsingArraysCopyOf(Integer[] srcArray, int elementToAdd) {
9+
Integer[] destArray = Arrays.copyOf(srcArray, srcArray.length + 1);
10+
11+
destArray[destArray.length - 1] = elementToAdd;
12+
return destArray;
13+
}
14+
15+
public Integer[] addElementUsingArrayList(Integer[] srcArray, int elementToAdd) {
16+
Integer[] destArray = new Integer[srcArray.length + 1];
17+
18+
ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(srcArray));
19+
arrayList.add(elementToAdd);
20+
21+
return arrayList.toArray(destArray);
22+
}
23+
24+
public Integer[] addElementUsingSystemArrayCopy(Integer[] srcArray, int elementToAdd) {
25+
Integer[] destArray = new Integer[srcArray.length + 1];
26+
27+
System.arraycopy(srcArray, 0, destArray, 0, srcArray.length);
28+
29+
destArray[destArray.length - 1] = elementToAdd;
30+
31+
return destArray;
32+
}
33+
34+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.array;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertArrayEquals;
7+
8+
public class AddElementToEndOfArrayUnitTest {
9+
10+
AddElementToEndOfArray addElementToEndOfArray;
11+
12+
@Before
13+
public void init() {
14+
addElementToEndOfArray = new AddElementToEndOfArray();
15+
}
16+
17+
@Test
18+
public void givenSourceArrayAndElement_whenAddElementUsingArraysCopyIsInvoked_thenNewElementMustBeAdded() {
19+
Integer[] sourceArray = {1, 2, 3, 4};
20+
int elementToAdd = 5;
21+
22+
Integer[] destArray = addElementToEndOfArray.addElementUsingArraysCopyOf(sourceArray, elementToAdd);
23+
24+
Integer[] expectedArray = {1, 2, 3, 4, 5};
25+
assertArrayEquals(expectedArray, destArray);
26+
}
27+
28+
@Test
29+
public void givenSourceArrayAndElement_whenAddElementUsingArrayListIsInvoked_thenNewElementMustBeAdded() {
30+
Integer[] sourceArray = {1, 2, 3, 4};
31+
int elementToAdd = 5;
32+
33+
Integer[] destArray = addElementToEndOfArray.addElementUsingArrayList(sourceArray, elementToAdd);
34+
35+
Integer[] expectedArray = {1, 2, 3, 4, 5};
36+
assertArrayEquals(expectedArray, destArray);
37+
}
38+
39+
@Test
40+
public void givenSourceArrayAndElement_whenAddElementUsingSystemArrayCopyIsInvoked_thenNewElementMustBeAdded() {
41+
Integer[] sourceArray = {1, 2, 3, 4};
42+
int elementToAdd = 5;
43+
44+
Integer[] destArray = addElementToEndOfArray.addElementUsingSystemArrayCopy(sourceArray, elementToAdd);
45+
46+
Integer[] expectedArray = {1, 2, 3, 4, 5};
47+
assertArrayEquals(expectedArray, destArray);
48+
}
49+
}

0 commit comments

Comments
 (0)