Skip to content

Commit a636f75

Browse files
Ahmed Tawilapivovarit
authored andcommitted
BAEL-1250 Initializing Arrays in Java (eugenp#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
1 parent 1c94773 commit a636f75

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

‎core-java/src/main/java/com/baeldung/array/ArrayInitializer.java‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Arrays;
44

5+
import org.apache.commons.lang.ArrayUtils;
6+
57
public class ArrayInitializer {
68

79
static int[] initializeArrayInLoop() {
@@ -24,17 +26,17 @@ static int[][] initializeMultiDimensionalArrayInLoop() {
2426
}
2527

2628
static String[] initializeArrayAtTimeOfDeclarationMethod1() {
27-
String array[] = new String[]{"Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda"};
29+
String array[] = new String[] { "Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda" };
2830
return array;
2931
}
3032

3133
static int[] initializeArrayAtTimeOfDeclarationMethod2() {
32-
int[] array = new int[]{1, 2, 3, 4, 5};
34+
int[] array = new int[] { 1, 2, 3, 4, 5 };
3335
return array;
3436
}
3537

3638
static int[] initializeArrayAtTimeOfDeclarationMethod3() {
37-
int array[] = {1, 2, 3, 4, 5};
39+
int array[] = { 1, 2, 3, 4, 5 };
3840
return array;
3941
}
4042

@@ -51,13 +53,13 @@ static int[] initializeArrayRangeUsingArraysFill() {
5153
}
5254

5355
static int[] initializeArrayUsingArraysCopy() {
54-
int array[] = {1, 2, 3, 4, 5};
56+
int array[] = { 1, 2, 3, 4, 5 };
5557
int[] copy = Arrays.copyOf(array, 5);
5658
return copy;
5759
}
5860

5961
static int[] initializeLargerArrayUsingArraysCopy() {
60-
int array[] = {1, 2, 3, 4, 5};
62+
int array[] = { 1, 2, 3, 4, 5 };
6163
int[] copy = Arrays.copyOf(array, 6);
6264
return copy;
6365
}
@@ -68,4 +70,9 @@ static int[] initializeArrayUsingArraysSetAll() {
6870
Arrays.setAll(array, p -> p > 9 ? 0 : p);
6971
return array;
7072
}
73+
74+
static char[] initializeArrayUsingArraysUtilClone() {
75+
char[] array = new char[] { 'a', 'b', 'c' };
76+
return ArrayUtils.clone(array);
77+
}
7178
}

‎core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysCopy;
99
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysFill;
1010
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysSetAll;
11+
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysUtilClone;
1112
import static com.baeldung.array.ArrayInitializer.initializeLargerArrayUsingArraysCopy;
1213
import static com.baeldung.array.ArrayInitializer.initializeMultiDimensionalArrayInLoop;
1314
import static org.junit.Assert.assertArrayEquals;
@@ -65,4 +66,9 @@ public void whenInitializeLargerArrayRangeUsingArraysCopy_thenCorrect() {
6566
public void whenInitializeLargerArrayRangeUsingArraysSetAll_thenCorrect() {
6667
assertArrayEquals(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, initializeArrayUsingArraysSetAll());
6768
}
69+
70+
@Test
71+
public void whenInitializeArrayUsingArraysUtilClone_thenCorrect() {
72+
assertArrayEquals(new char[] { 'a', 'b', 'c' }, initializeArrayUsingArraysUtilClone());
73+
}
6874
}

0 commit comments

Comments
 (0)