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:
parent
1c9477390d
commit
a636f754b0
|
@ -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 {
|
||||||
|
|
||||||
static int[] initializeArrayInLoop() {
|
static int[] initializeArrayInLoop() {
|
||||||
|
@ -24,17 +26,17 @@ public class ArrayInitializer {
|
||||||
}
|
}
|
||||||
|
|
||||||
static String[] initializeArrayAtTimeOfDeclarationMethod1() {
|
static String[] initializeArrayAtTimeOfDeclarationMethod1() {
|
||||||
String array[] = new String[]{"Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda"};
|
String array[] = new String[] { "Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda" };
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int[] initializeArrayAtTimeOfDeclarationMethod2() {
|
static int[] initializeArrayAtTimeOfDeclarationMethod2() {
|
||||||
int[] array = new int[]{1, 2, 3, 4, 5};
|
int[] array = new int[] { 1, 2, 3, 4, 5 };
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int[] initializeArrayAtTimeOfDeclarationMethod3() {
|
static int[] initializeArrayAtTimeOfDeclarationMethod3() {
|
||||||
int array[] = {1, 2, 3, 4, 5};
|
int array[] = { 1, 2, 3, 4, 5 };
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,13 +53,13 @@ public class ArrayInitializer {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int[] initializeArrayUsingArraysCopy() {
|
static int[] initializeArrayUsingArraysCopy() {
|
||||||
int array[] = {1, 2, 3, 4, 5};
|
int array[] = { 1, 2, 3, 4, 5 };
|
||||||
int[] copy = Arrays.copyOf(array, 5);
|
int[] copy = Arrays.copyOf(array, 5);
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int[] initializeLargerArrayUsingArraysCopy() {
|
static int[] initializeLargerArrayUsingArraysCopy() {
|
||||||
int array[] = {1, 2, 3, 4, 5};
|
int array[] = { 1, 2, 3, 4, 5 };
|
||||||
int[] copy = Arrays.copyOf(array, 6);
|
int[] copy = Arrays.copyOf(array, 6);
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
@ -68,4 +70,9 @@ public class ArrayInitializer {
|
||||||
Arrays.setAll(array, p -> p > 9 ? 0 : p);
|
Arrays.setAll(array, p -> p > 9 ? 0 : p);
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char[] initializeArrayUsingArraysUtilClone() {
|
||||||
|
char[] array = new char[] { 'a', 'b', 'c' };
|
||||||
|
return ArrayUtils.clone(array);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,9 @@ 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenInitializeArrayUsingArraysUtilClone_thenCorrect() {
|
||||||
|
assertArrayEquals(new char[] { 'a', 'b', 'c' }, initializeArrayUsingArraysUtilClone());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue