Refactor Arrays (#2857)

This commit is contained in:
Grzegorz Piwowarek 2017-10-25 12:08:34 +02:00 committed by GitHub
parent a5e2357033
commit 1757625dba
1 changed files with 23 additions and 26 deletions

View File

@ -4,71 +4,68 @@ import java.util.Arrays;
public class ArrayInitializer { public class ArrayInitializer {
public static int[] initializeArrayInLoop() { static int[] initializeArrayInLoop() {
int array[] = new int[5]; int array[] = new int[5];
for (int i = 0; i < array.length; i++) for (int i = 0; i < array.length; i++) {
array[i] = i + 2; array[i] = i + 2;
}
return array; return array;
} }
public static int[][] initializeMultiDimensionalArrayInLoop() { static int[][] initializeMultiDimensionalArrayInLoop() {
int array[][] = new int[2][5]; int array[][] = new int[2][5];
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++) {
for (int j = 0; j < 5; j++) for (int j = 0; j < 5; j++) {
array[i][j] = j + 1; array[i][j] = j + 1;
}
}
return array; return array;
} }
public 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;
} }
public 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;
} }
public static int[] initializeArrayAtTimeOfDeclarationMethod3() { static int[] initializeArrayAtTimeOfDeclarationMethod3() {
int array[] = { 1, 2, 3, 4, 5 }; int array[] = {1, 2, 3, 4, 5};
return array; return array;
} }
public static long[] initializeArrayUsingArraysFill() { static long[] initializeArrayUsingArraysFill() {
long array[] = new long[5]; long array[] = new long[5];
Arrays.fill(array, 30); Arrays.fill(array, 30);
return array; return array;
} }
public static int[] initializeArrayRangeUsingArraysFill() { static int[] initializeArrayRangeUsingArraysFill() {
int array[] = new int[5]; int array[] = new int[5];
Arrays.fill(array, 0, 3, -50); Arrays.fill(array, 0, 3, -50);
return array; return array;
} }
public 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;
} }
public 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;
} }
public static int[] initializeArrayUsingArraysSetAll() { static int[] initializeArrayUsingArraysSetAll() {
int[] array = new int[20]; int[] array = new int[20];
for (int i = 0; i < 20; i++) { Arrays.setAll(array, p -> p > 9 ? 0 : p);
Arrays.setAll(array, p -> {
if (p > 9)
return 0;
else
return p;
});
}
return array; return array;
} }
} }