BAEL-1250 Initializing Arrays in Java

This commit is contained in:
Ahmed Tawila 2017-10-22 10:48:39 +02:00
parent 9c84c85137
commit fdd485ddd9
2 changed files with 96 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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());
}
}