BAEL-1250 Initializing Arrays in Java (#2811)

* Evaluation article: Different Types of Bean Injection in Spring

* added tests & changed configuration to Java-based config

* removed xml config files

* rename unit tests

* BAEL-972 - Apache Commons Text

* remove code from evaluation article

* remove code from evaluation article

* BAEL-972 - Apache Commons Text - added another example

* BAEL-972 - Apache Commons Text - just indentation

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java - fix problems

* BAEL-1033 Introduction to StreamUtils

* BAEL-1033 Introduction to StreamUtils

* BAEL-1033 Introduction to StreamUtils

* fix formatting

* BAEL-1033 minor refactor

* BAEL-1035 Introduction to Eclipse Collections

* format

* BAEL-1035 Introduction to Eclipse Collections

* BAEL-1035 Introduction to Eclipse Collections

* BAEL-1035 Introduction to Eclipse Collections

* cleanup

* cleanup

* BAEL-1109 Introduction to JCache

* BAEL-1109 Introduction to JCache

* remove unneeded property in pom.xml

* fix formatting

* close cache instances properly

* remove latest commit

* BAEL-1057 Introduction to rxjava-jdbc

* refactor rxjava-jdbc

* Refactor rxjava-jdbc

* Refactoring rxjava-jdbc

* BAEL-1171 java.lang.String API

* refactor rxjava-jdbc

* refactor String

* String API - move multiple classes into a single class

* move class into test package

* BAEL-1171 String.lang.String API

* BAEL-1171 java.lang.String API

* BAEL-1250 Initializing Arrays in Java

* BAEL-1250 Initializing Arrays in Java
This commit is contained in:
Ahmed Tawila 2017-10-24 18:47:40 +02:00 committed by Grzegorz Piwowarek
parent 5d39af398f
commit a5e2357033
2 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,74 @@
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;
}
public static int[] initializeArrayUsingArraysCopy() {
int array[] = { 1, 2, 3, 4, 5 };
int[] copy = Arrays.copyOf(array, 5);
return copy;
}
public static int[] initializeLargerArrayUsingArraysCopy() {
int array[] = { 1, 2, 3, 4, 5 };
int[] copy = Arrays.copyOf(array, 6);
return copy;
}
public static int[] initializeArrayUsingArraysSetAll() {
int[] array = new int[20];
for (int i = 0; i < 20; i++) {
Arrays.setAll(array, p -> {
if (p > 9)
return 0;
else
return p;
});
}
return array;
}
}

View File

@ -0,0 +1,68 @@
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.initializeArrayUsingArraysCopy;
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysFill;
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysSetAll;
import static com.baeldung.array.ArrayInitializer.initializeLargerArrayUsingArraysCopy;
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());
}
@Test
public void whenInitializeArrayUsingArraysFill_thenCorrect() {
assertArrayEquals(new long[] { 30, 30, 30, 30, 30 }, initializeArrayUsingArraysFill());
}
@Test
public void whenInitializeArrayRangeUsingArraysFill_thenCorrect() {
assertArrayEquals(new int[] { -50, -50, -50, 0, 0 }, initializeArrayRangeUsingArraysFill());
}
@Test
public void whenInitializeArrayRangeUsingArraysCopy_thenCorrect() {
assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, initializeArrayUsingArraysCopy());
}
@Test
public void whenInitializeLargerArrayRangeUsingArraysCopy_thenCorrect() {
assertArrayEquals(new int[] { 1, 2, 3, 4, 5, 0 }, initializeLargerArrayUsingArraysCopy());
}
@Test
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());
}
}