From e8b27416d72a243c53cfe309dde5c23966c559b1 Mon Sep 17 00:00:00 2001 From: "Kai.Yuan" Date: Fri, 2 Feb 2024 17:06:31 +0100 Subject: [PATCH 1/2] [mv-0-to-end-array] move zeros to the end --- .../MoveZeroesToTheEndOfAnArrayUnitTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 core-java-modules/core-java-arrays-operations-advanced-2/src/test/java/com/baeldung/movezerototheend/MoveZeroesToTheEndOfAnArrayUnitTest.java diff --git a/core-java-modules/core-java-arrays-operations-advanced-2/src/test/java/com/baeldung/movezerototheend/MoveZeroesToTheEndOfAnArrayUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced-2/src/test/java/com/baeldung/movezerototheend/MoveZeroesToTheEndOfAnArrayUnitTest.java new file mode 100644 index 0000000000..ae0152feca --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced-2/src/test/java/com/baeldung/movezerototheend/MoveZeroesToTheEndOfAnArrayUnitTest.java @@ -0,0 +1,40 @@ +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 { + static final int[] EXPECTED = new int[] { 1, 2, 3, 4, 0, 0 }; + + @Test + void whenCreatingANewArrayAndCopyingValues_thenGetTheExpectedResult() { + int[] array = new int[] { 1, 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[] { 1, 2, 0, 3, 4, 0 }; + int idx = 0; + for (int n : array) { + if (n != 0) { + array[idx++] = n; + } + System.out.println(Arrays.toString(array)); + } + while (idx < array.length) { + array[idx++] = 0; + } + assertArrayEquals(EXPECTED, array); + } +} \ No newline at end of file From 7b115b3a269d825dae6650954bdb749c73a2125d Mon Sep 17 00:00:00 2001 From: "Kai.Yuan" Date: Sun, 4 Feb 2024 19:24:20 +0100 Subject: [PATCH 2/2] [mv-0-to-end-array] changes for comments --- .../MoveZeroesToTheEndOfAnArrayUnitTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core-java-modules/core-java-arrays-operations-advanced-2/src/test/java/com/baeldung/movezerototheend/MoveZeroesToTheEndOfAnArrayUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced-2/src/test/java/com/baeldung/movezerototheend/MoveZeroesToTheEndOfAnArrayUnitTest.java index ae0152feca..1d56e1f50b 100644 --- a/core-java-modules/core-java-arrays-operations-advanced-2/src/test/java/com/baeldung/movezerototheend/MoveZeroesToTheEndOfAnArrayUnitTest.java +++ b/core-java-modules/core-java-arrays-operations-advanced-2/src/test/java/com/baeldung/movezerototheend/MoveZeroesToTheEndOfAnArrayUnitTest.java @@ -7,11 +7,11 @@ import java.util.Arrays; import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class MoveZeroesToTheEndOfAnArrayUnitTest { - static final int[] EXPECTED = new int[] { 1, 2, 3, 4, 0, 0 }; + private static final int[] EXPECTED = new int[] { 42, 2, 3, 4, 0, 0 }; @Test void whenCreatingANewArrayAndCopyingValues_thenGetTheExpectedResult() { - int[] array = new int[] { 1, 2, 0, 3, 4, 0 }; + int[] array = new int[] { 42, 2, 0, 3, 4, 0 }; int[] result = new int[array.length]; int idx = 0; for (int n : array) { @@ -24,13 +24,12 @@ public class MoveZeroesToTheEndOfAnArrayUnitTest { @Test void whenMovingZeroInTheOriginalArray_thenGetTheExpectedResult() { - int[] array = new int[] { 1, 2, 0, 3, 4, 0 }; + int[] array = new int[] { 42, 2, 0, 3, 4, 0 }; int idx = 0; for (int n : array) { if (n != 0) { array[idx++] = n; } - System.out.println(Arrays.toString(array)); } while (idx < array.length) { array[idx++] = 0;