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:
commit
6ada5de93e
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue