diff --git a/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java b/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java new file mode 100644 index 0000000000..0c9bf1834e --- /dev/null +++ b/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java @@ -0,0 +1,48 @@ +package com.baeldung.array; + +import java.util.Arrays; + +public class ArrayInitializer { + + public static int[] initializeArrayInLoop() { + int array[] = new int[5]; + for (int i = 0; i < array.length; i++) + array[i] = i + 2; + return array; + } + + public static int[][] initializeMultiDimensionalArrayInLoop() { + int array[][] = new int[2][5]; + for (int i = 0; i < 2; i++) + for (int j = 0; j < 5; j++) + array[i][j] = j + 1; + return array; + } + + public static String[] initializeArrayAtTimeOfDeclarationMethod1() { + String array[] = new String[] { "Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda" }; + return array; + } + + public static int[] initializeArrayAtTimeOfDeclarationMethod2() { + int[] array = new int[] { 1, 2, 3, 4, 5 }; + return array; + } + + public static int[] initializeArrayAtTimeOfDeclarationMethod3() { + int array[] = { 1, 2, 3, 4, 5 }; + return array; + } + + public static long[] initializeArrayUsingArraysFill() { + long array[] = new long[5]; + Arrays.fill(array, 30); + return array; + } + + public static int[] initializeArrayRangeUsingArraysFill() { + int array[] = new int[5]; + Arrays.fill(array, 0, 3, -50); + 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 new file mode 100644 index 0000000000..0797a524c4 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java @@ -0,0 +1,48 @@ +package com.baeldung.array; + +import static com.baeldung.array.ArrayInitializer.initializeArrayAtTimeOfDeclarationMethod1; +import static com.baeldung.array.ArrayInitializer.initializeArrayAtTimeOfDeclarationMethod2; +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.initializeArrayUsingArraysFill; +import static com.baeldung.array.ArrayInitializer.initializeMultiDimensionalArrayInLoop; +import static org.junit.Assert.assertArrayEquals; + +import org.junit.Test; + +public class ArrayInitializerTest { + + @Test + public void whenInitializeArrayInLoop_thenCorrect() { + assertArrayEquals(new int[] { 2, 3, 4, 5, 6 }, initializeArrayInLoop()); + } + + @Test + public void whenInitializeMultiDimensionalArrayInLoop_thenCorrect() { + assertArrayEquals(new int[][] { { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 } }, initializeMultiDimensionalArrayInLoop()); + } + + @Test + public void whenInitializeArrayAtTimeOfDeclarationMethod1_thenCorrect() { + assertArrayEquals(new String[] { "Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda" }, initializeArrayAtTimeOfDeclarationMethod1()); + } + + @Test + public void whenInitializeArrayAtTimeOfDeclarationMethod2_thenCorrect() { + assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, initializeArrayAtTimeOfDeclarationMethod2()); + } + + @Test + public void whenInitializeArrayAtTimeOfDeclarationMethod3_thenCorrect() { + assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, initializeArrayAtTimeOfDeclarationMethod3()); + } + + public void whenInitializeArrayUsingArraysFill_thenCorrect() { + assertArrayEquals(new long[] { 30, 30, 30, 30, 30 }, initializeArrayUsingArraysFill()); + } + + public void whenInitializeArrayRangeUsingArraysFill() { + assertArrayEquals(new int[] { -50, -50, -50, 0, 0 }, initializeArrayRangeUsingArraysFill()); + } +}