BAEL-1250 Initializing Arrays in Java

This commit is contained in:
Ahmed Tawila 2017-10-25 15:34:13 +02:00
parent 595f6e3de0
commit 26f495afa7
2 changed files with 12 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package com.baeldung.array;
import java.util.Arrays; import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;
public class ArrayInitializer { public class ArrayInitializer {
public static int[] initializeArrayInLoop() { public static int[] initializeArrayInLoop() {
@ -71,4 +73,9 @@ public class ArrayInitializer {
} }
return array; return array;
} }
public static char[] initializeArrayUsingArraysUtilClone() {
char[] array = new char[] {'a', 'b', 'c'};
return ArrayUtils.clone(array);
}
} }

View File

@ -8,6 +8,7 @@ import static com.baeldung.array.ArrayInitializer.initializeArrayRangeUsingArray
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysCopy; import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysCopy;
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysFill; import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysFill;
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysSetAll; 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.initializeLargerArrayUsingArraysCopy;
import static com.baeldung.array.ArrayInitializer.initializeMultiDimensionalArrayInLoop; import static com.baeldung.array.ArrayInitializer.initializeMultiDimensionalArrayInLoop;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
@ -65,4 +66,8 @@ public class ArrayInitializerTest {
public void whenInitializeLargerArrayRangeUsingArraysSetAll_thenCorrect() { 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()); 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());
}
} }