BAEL-1250 Initializing Arrays in Java (#2862)

* 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

* BAEL-1250 Initializing Arrays in Java

* small fix
This commit is contained in:
Ahmed Tawila 2017-10-25 19:17:15 +02:00 committed by Grzegorz Piwowarek
parent 1c9477390d
commit a636f754b0
2 changed files with 18 additions and 5 deletions

View File

@ -2,6 +2,8 @@ package com.baeldung.array;
import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;
public class ArrayInitializer {
static int[] initializeArrayInLoop() {
@ -24,17 +26,17 @@ public class ArrayInitializer {
}
static String[] initializeArrayAtTimeOfDeclarationMethod1() {
String array[] = new String[]{"Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda"};
String array[] = new String[] { "Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda" };
return array;
}
static int[] initializeArrayAtTimeOfDeclarationMethod2() {
int[] array = new int[]{1, 2, 3, 4, 5};
int[] array = new int[] { 1, 2, 3, 4, 5 };
return array;
}
static int[] initializeArrayAtTimeOfDeclarationMethod3() {
int array[] = {1, 2, 3, 4, 5};
int array[] = { 1, 2, 3, 4, 5 };
return array;
}
@ -51,13 +53,13 @@ public class ArrayInitializer {
}
static int[] initializeArrayUsingArraysCopy() {
int array[] = {1, 2, 3, 4, 5};
int array[] = { 1, 2, 3, 4, 5 };
int[] copy = Arrays.copyOf(array, 5);
return copy;
}
static int[] initializeLargerArrayUsingArraysCopy() {
int array[] = {1, 2, 3, 4, 5};
int array[] = { 1, 2, 3, 4, 5 };
int[] copy = Arrays.copyOf(array, 6);
return copy;
}
@ -68,4 +70,9 @@ public class ArrayInitializer {
Arrays.setAll(array, p -> p > 9 ? 0 : p);
return array;
}
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.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,9 @@ 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());
}
@Test
public void whenInitializeArrayUsingArraysUtilClone_thenCorrect() {
assertArrayEquals(new char[] { 'a', 'b', 'c' }, initializeArrayUsingArraysUtilClone());
}
}