From 26f495afa7dd942bd2de3165b6c4ff5b409837f0 Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Wed, 25 Oct 2017 15:34:13 +0200 Subject: [PATCH] BAEL-1250 Initializing Arrays in Java --- .../src/main/java/com/baeldung/array/ArrayInitializer.java | 7 +++++++ .../test/java/com/baeldung/array/ArrayInitializerTest.java | 5 +++++ 2 files changed, 12 insertions(+) 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 fd00c74e7f..7759e282e6 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 { public static int[] initializeArrayInLoop() { @@ -71,4 +73,9 @@ public class ArrayInitializer { } return array; } + + public 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..6826a5186b 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,8 @@ 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()); } + + public void whenInitializeArrayUsingArraysUtilClone_thenCorrect() { + assertArrayEquals(new char[] { 'a', 'b', 'c' }, initializeArrayUsingArraysUtilClone()); + } }