Skip to content

Commit 4f52267

Browse files
authored
[rm-all-el-array] remove all elements from an array (#17351)
1 parent d26c120 commit 4f52267

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.array.removeallelements;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import java.util.Arrays;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
public class RemoveAllElementsFromStringArrayUnitTest {
11+
12+
private static final String[] SIX_NULL_ARRAY = new String[] { null, null, null, null, null, null };
13+
14+
@Test
15+
void whenReassignNonFinalArray_thenCorrect() {
16+
String[] myArray1 = new String[] { "Java", "Kotlin", "Ruby", "Go", "C#", "C++" };
17+
myArray1 = new String[0];
18+
assertEquals(0, myArray1.length);
19+
20+
String[] myArray2 = new String[] { "Arch Linux", "Debian", "CentOS", "Gentoo", "Fedora", "Redhat" };
21+
myArray2 = new String[myArray2.length];
22+
assertArrayEquals(SIX_NULL_ARRAY, myArray2);
23+
}
24+
25+
@Test
26+
void whenResettingAllElementsToNull_thenCorrect() {
27+
final String[] myArray = new String[] { "A", "B", "C", "D", "E", "F" };
28+
for (int i = 0; i < myArray.length; i++) {
29+
myArray[i] = null;
30+
}
31+
assertArrayEquals(SIX_NULL_ARRAY, myArray);
32+
}
33+
34+
@Test
35+
void whenUsingArraysFill_thenCorrect() {
36+
final String[] myArray = new String[] { "a", "b", "c", "d", "e", "f" };
37+
Arrays.fill(myArray, null);
38+
assertArrayEquals(SIX_NULL_ARRAY, myArray);
39+
}
40+
}

0 commit comments

Comments
 (0)