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 cd6d524ad2..0ba6c342d9 100644 --- a/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java +++ b/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java @@ -2,6 +2,8 @@ package com.baeldung.array; import java.util.Arrays; +import org.apache.commons.lang.ArrayUtils; + public class ArrayInitializer { static int[] initializeArrayInLoop() { @@ -24,17 +26,17 @@ public class ArrayInitializer { } static String[] initializeArrayAtTimeOfDeclarationMethod1() { - String array[] = new String[]{"Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda"}; + String array[] = new String[] { "Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda" }; return array; } static int[] initializeArrayAtTimeOfDeclarationMethod2() { - int[] array = new int[]{1, 2, 3, 4, 5}; + int[] array = new int[] { 1, 2, 3, 4, 5 }; return array; } static int[] initializeArrayAtTimeOfDeclarationMethod3() { - int array[] = {1, 2, 3, 4, 5}; + int array[] = { 1, 2, 3, 4, 5 }; return array; } @@ -51,13 +53,13 @@ public class ArrayInitializer { } static int[] initializeArrayUsingArraysCopy() { - int array[] = {1, 2, 3, 4, 5}; + int array[] = { 1, 2, 3, 4, 5 }; int[] copy = Arrays.copyOf(array, 5); return copy; } static int[] initializeLargerArrayUsingArraysCopy() { - int array[] = {1, 2, 3, 4, 5}; + int array[] = { 1, 2, 3, 4, 5 }; int[] copy = Arrays.copyOf(array, 6); return copy; } @@ -68,4 +70,9 @@ public class ArrayInitializer { Arrays.setAll(array, p -> p > 9 ? 0 : p); return array; } + + static char[] initializeArrayUsingArraysUtilClone() { + char[] array = new char[] { 'a', 'b', 'c' }; + return ArrayUtils.clone(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 d3afad7b00..7265fa20e5 100644 --- a/core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java +++ b/core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java @@ -8,6 +8,7 @@ import static com.baeldung.array.ArrayInitializer.initializeArrayRangeUsingArray 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.initializeArrayUsingArraysUtilClone; import static com.baeldung.array.ArrayInitializer.initializeLargerArrayUsingArraysCopy; import static com.baeldung.array.ArrayInitializer.initializeMultiDimensionalArrayInLoop; import static org.junit.Assert.assertArrayEquals; @@ -65,4 +66,9 @@ public class ArrayInitializerTest { 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()); } + + @Test + public void whenInitializeArrayUsingArraysUtilClone_thenCorrect() { + assertArrayEquals(new char[] { 'a', 'b', 'c' }, initializeArrayUsingArraysUtilClone()); + } }