Skip to content

Commit 0cafd30

Browse files
kiviewKevinGilmore
authored andcommitted
Example for removing first element of array (BAEL-2029) (eugenp#4836)
* Example for removing first element of array (BAEL-2029) * Use AssertJ assertions
1 parent 46cf3c3 commit 0cafd30

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.array;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
public class RemoveFirstElementUnitTest {
13+
14+
@Test
15+
public void givenStringArray_whenRemovingFirstElement_thenArrayIsSmallerAndElementRemoved() {
16+
String[] stringArray = {"foo", "bar", "baz"};
17+
18+
String[] modifiedArray = Arrays.copyOfRange(stringArray, 1, stringArray.length);
19+
20+
assertThat(modifiedArray.length).isEqualTo(2);
21+
assertThat(modifiedArray[0]).isEqualTo("bar");
22+
}
23+
24+
@Test
25+
public void givenArrayList_whenRemovingFirstElement_thenListSmallerAndElementRemoved() {
26+
List<String> stringList = new ArrayList<>(Arrays.asList("foo", "bar", "baz"));
27+
stringList.remove(0);
28+
29+
assertThat(stringList.size()).isEqualTo(2);
30+
assertThat(stringList.get(0)).isEqualTo("bar");
31+
}
32+
33+
@Test
34+
public void givenLinkedList_whenRemovingFirstElement_thenListSmallerAndElementRemoved() {
35+
List<String> stringList = new LinkedList<>(Arrays.asList("foo", "bar", "baz"));
36+
stringList.remove(0);
37+
38+
assertThat(stringList.size()).isEqualTo(2);
39+
assertThat(stringList.get(0)).isEqualTo("bar");
40+
}
41+
42+
}

0 commit comments

Comments
 (0)