Refactor Arrays (#2857)
This commit is contained in:
parent
a5e2357033
commit
1757625dba
|
@ -4,71 +4,68 @@ import java.util.Arrays;
|
|||
|
||||
public class ArrayInitializer {
|
||||
|
||||
public static int[] initializeArrayInLoop() {
|
||||
static int[] initializeArrayInLoop() {
|
||||
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;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static int[][] initializeMultiDimensionalArrayInLoop() {
|
||||
static int[][] initializeMultiDimensionalArrayInLoop() {
|
||||
int array[][] = new int[2][5];
|
||||
for (int i = 0; i < 2; i++)
|
||||
for (int j = 0; j < 5; j++)
|
||||
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" };
|
||||
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 };
|
||||
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 };
|
||||
static int[] initializeArrayAtTimeOfDeclarationMethod3() {
|
||||
int array[] = {1, 2, 3, 4, 5};
|
||||
return array;
|
||||
}
|
||||
|
||||
public static long[] initializeArrayUsingArraysFill() {
|
||||
static long[] initializeArrayUsingArraysFill() {
|
||||
long array[] = new long[5];
|
||||
Arrays.fill(array, 30);
|
||||
return array;
|
||||
}
|
||||
|
||||
public static int[] initializeArrayRangeUsingArraysFill() {
|
||||
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 };
|
||||
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 };
|
||||
static int[] initializeLargerArrayUsingArraysCopy() {
|
||||
int array[] = {1, 2, 3, 4, 5};
|
||||
int[] copy = Arrays.copyOf(array, 6);
|
||||
return copy;
|
||||
}
|
||||
|
||||
public static int[] initializeArrayUsingArraysSetAll() {
|
||||
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;
|
||||
});
|
||||
}
|
||||
Arrays.setAll(array, p -> p > 9 ? 0 : p);
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue