Merge pull request #15815 from sk1418/mv-0-to-end-array

[mv-0-to-end-array] move zeros to the end
This commit is contained in:
Liam Williams 2024-02-09 16:13:15 +00:00 committed by GitHub
commit 6ada5de93e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package com.baeldung.movezerototheend;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class MoveZeroesToTheEndOfAnArrayUnitTest {
private static final int[] EXPECTED = new int[] { 42, 2, 3, 4, 0, 0 };
@Test
void whenCreatingANewArrayAndCopyingValues_thenGetTheExpectedResult() {
int[] array = new int[] { 42, 2, 0, 3, 4, 0 };
int[] result = new int[array.length];
int idx = 0;
for (int n : array) {
if (n != 0) {
result[idx++] = n;
}
}
assertArrayEquals(EXPECTED, result);
}
@Test
void whenMovingZeroInTheOriginalArray_thenGetTheExpectedResult() {
int[] array = new int[] { 42, 2, 0, 3, 4, 0 };
int idx = 0;
for (int n : array) {
if (n != 0) {
array[idx++] = n;
}
}
while (idx < array.length) {
array[idx++] = 0;
}
assertArrayEquals(EXPECTED, array);
}
}