Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
9f87819
Evaluation article: Different Types of Bean Injection in Spring
Jun 12, 2017
ac2ce96
added tests & changed configuration to Java-based config
Jun 16, 2017
5a8ec66
removed xml config files
Jun 16, 2017
ee8bf9f
rename unit tests
Jun 16, 2017
94fc522
BAEL-972 - Apache Commons Text
Jun 30, 2017
e76cf9c
remove code from evaluation article
Jun 30, 2017
c26eaf8
remove code from evaluation article
Jun 30, 2017
0dadce5
Merge branch 'master' into master
Jun 30, 2017
760d4e1
BAEL-972 - Apache Commons Text - added another example
Jul 1, 2017
7d87b3a
BAEL-972 - Apache Commons Text - just indentation
Jul 2, 2017
d184aa3
BAEL-994 - TemporalAdjuster in Java
Jul 8, 2017
1e9cccb
Merge branch 'master' into master
Jul 8, 2017
236ac77
BAEL-994 - TemporalAdjuster in Java
Jul 8, 2017
32d4dfa
Merge branch 'master' of https://github.com/ahmedtawila/tutorials
Jul 8, 2017
54debec
Merge branch 'master' into master
Jul 8, 2017
62efecc
BAEL-994 - TemporalAdjuster in Java
Jul 8, 2017
dc9ee11
BAEL-994 - TemporalAdjuster in Java
Jul 8, 2017
7e1f418
Merge branch 'master' of https://github.com/ahmedtawila/tutorials
Jul 8, 2017
0b60c21
BAEL-994 - TemporalAdjuster in Java - fix problems
Jul 8, 2017
344fdce
BAEL-1033 Introduction to StreamUtils
Jul 30, 2017
2b535e4
Merge branch 'master' into master
Jul 30, 2017
ece39cb
BAEL-1033 Introduction to StreamUtils
Jul 30, 2017
c41a54d
Merge branch 'master' of https://github.com/ahmedtawila/tutorials
Jul 30, 2017
52745f2
BAEL-1033 Introduction to StreamUtils
Jul 30, 2017
4f2857d
fix formatting
Jul 30, 2017
af4a8eb
BAEL-1033 minor refactor
Jul 30, 2017
207f88b
BAEL-1035 Introduction to Eclipse Collections
Aug 9, 2017
1fdbaa2
Merge branch 'master' into master
Aug 9, 2017
66eea4c
format
Aug 9, 2017
ac1e77f
Merge branch 'master' of https://github.com/ahmedtawila/tutorials
Aug 9, 2017
c8a6913
BAEL-1035 Introduction to Eclipse Collections
Aug 9, 2017
4c2447e
BAEL-1035 Introduction to Eclipse Collections
Aug 11, 2017
e83b5c9
Merge branch 'master' into master
Aug 11, 2017
81cdec1
BAEL-1035 Introduction to Eclipse Collections
Aug 12, 2017
f704585
Merge branch 'master' of https://github.com/ahmedtawila/tutorials
Aug 12, 2017
696d056
Merge branch 'master' into master
Aug 12, 2017
c5433e8
cleanup
Aug 12, 2017
92f9bc5
Merge branch 'master' of https://github.com/ahmedtawila/tutorials
Aug 12, 2017
a1b7c27
cleanup
Aug 12, 2017
d56dbc0
BAEL-1109 Introduction to JCache
Sep 9, 2017
1d90d18
BAEL-1109 Introduction to JCache
Sep 9, 2017
33dfe04
Merge branch 'master' into master
Sep 9, 2017
602279a
remove unneeded property in pom.xml
Sep 9, 2017
2ac0934
merge with remote
Sep 9, 2017
4647e1b
fix formatting
Sep 9, 2017
58aae2a
close cache instances properly
Sep 16, 2017
870b93d
remove latest commit
Sep 16, 2017
0bc5f8d
Merge branch 'master' into master
Sep 16, 2017
a6befdb
BAEL-1057 Introduction to rxjava-jdbc
Sep 23, 2017
7ef66d7
refactor rxjava-jdbc
Sep 23, 2017
c3c8c4f
Merge branch 'master' into master
Sep 23, 2017
b3edd4b
Refactor rxjava-jdbc
Sep 23, 2017
a4d1a89
Merge branch 'master' of https://github.com/ahmedtawila/tutorials
Sep 23, 2017
5253362
Refactoring rxjava-jdbc
Sep 23, 2017
9f9fa93
BAEL-1171 java.lang.String API
Sep 30, 2017
ecb0e99
refactor rxjava-jdbc
Sep 30, 2017
35182c4
refactor String
Sep 30, 2017
d559cf4
String API - move multiple classes into a single class
Oct 1, 2017
ab26dc0
move class into test package
Oct 1, 2017
07d5347
fix conflicts and merge with remote
Oct 7, 2017
6c6109b
Merge branch 'master' of https://github.com/eugenp/tutorials
Oct 9, 2017
d503a53
Merge branch 'master' of https://github.com/eugenp/tutorials
Oct 13, 2017
fe4bbb9
Merge branch 'master' of https://github.com/eugenp/tutorials
Oct 14, 2017
44fa64e
BAEL-1171 String.lang.String API
Oct 14, 2017
e2085cb
Merge branch 'master' of https://github.com/eugenp/tutorials
Oct 15, 2017
82fde89
BAEL-1171 java.lang.String API
Oct 15, 2017
3a1e13f
BAEL-1171 String.lang.String API
Oct 15, 2017
9c84c85
Merge branch 'master' of https://github.com/eugenp/tutorials
Oct 19, 2017
fdd485d
BAEL-1250 Initializing Arrays in Java
Oct 22, 2017
7120a2f
Merge branch 'master' of https://github.com/eugenp/tutorials
Oct 22, 2017
d14411e
BAEL-1250 Initializing Arrays in Java
Oct 23, 2017
c768592
Merge branch 'master' of https://github.com/eugenp/tutorials
Oct 23, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions core-java/src/main/java/com/baeldung/array/ArrayInitializer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}