diff --git a/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java b/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java index 0c9bf1834e..fd00c74e7f 100644 --- a/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java +++ b/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java @@ -45,4 +45,30 @@ public class ArrayInitializer { Arrays.fill(array, 0, 3, -50); return array; } + + public static int[] initializeArrayUsingArraysCopy() { + int array[] = { 1, 2, 3, 4, 5 }; + int[] copy = Arrays.copyOf(array, 5); + return copy; + } + + public static int[] initializeLargerArrayUsingArraysCopy() { + int array[] = { 1, 2, 3, 4, 5 }; + int[] copy = Arrays.copyOf(array, 6); + return copy; + } + + public static int[] initializeArrayUsingArraysSetAll() { + int[] array = new int[20]; + + for (int i = 0; i < 20; i++) { + Arrays.setAll(array, p -> { + if (p > 9) + return 0; + else + return p; + }); + } + return array; + } } diff --git a/core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java b/core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java index 0797a524c4..d3afad7b00 100644 --- a/core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java +++ b/core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java @@ -5,7 +5,10 @@ import static com.baeldung.array.ArrayInitializer.initializeArrayAtTimeOfDeclara import static com.baeldung.array.ArrayInitializer.initializeArrayAtTimeOfDeclarationMethod3; import static com.baeldung.array.ArrayInitializer.initializeArrayInLoop; import static com.baeldung.array.ArrayInitializer.initializeArrayRangeUsingArraysFill; +import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysCopy; import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysFill; +import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysSetAll; +import static com.baeldung.array.ArrayInitializer.initializeLargerArrayUsingArraysCopy; import static com.baeldung.array.ArrayInitializer.initializeMultiDimensionalArrayInLoop; import static org.junit.Assert.assertArrayEquals; @@ -38,11 +41,28 @@ public class ArrayInitializerTest { assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, initializeArrayAtTimeOfDeclarationMethod3()); } + @Test public void whenInitializeArrayUsingArraysFill_thenCorrect() { assertArrayEquals(new long[] { 30, 30, 30, 30, 30 }, initializeArrayUsingArraysFill()); } - public void whenInitializeArrayRangeUsingArraysFill() { + @Test + public void whenInitializeArrayRangeUsingArraysFill_thenCorrect() { assertArrayEquals(new int[] { -50, -50, -50, 0, 0 }, initializeArrayRangeUsingArraysFill()); } + + @Test + public void whenInitializeArrayRangeUsingArraysCopy_thenCorrect() { + assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, initializeArrayUsingArraysCopy()); + } + + @Test + public void whenInitializeLargerArrayRangeUsingArraysCopy_thenCorrect() { + assertArrayEquals(new int[] { 1, 2, 3, 4, 5, 0 }, initializeLargerArrayUsingArraysCopy()); + } + + @Test + public void whenInitializeLargerArrayRangeUsingArraysSetAll_thenCorrect() { + assertArrayEquals(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, initializeArrayUsingArraysSetAll()); + } }