This is my initial code,
public class Main {
public static void main(String[] args) {
// --------Declaring variables--------
int arrLength = 6;
String[] arr1 = new String[arrLength];
String[] arr2 = new String[arrLength];
String[] arr3 = new String[arrLength];
joinArrays(arr1,arr2,arr3);
}
private static void joinArrays(String[] arr1, String[] arr2, String[] arr3){
System.out.print("Array 1 : ");
System.out.println(String.join(",",arr1));
System.out.println("");
System.out.print("Array 2 : ");
System.out.println(String.join(",",arr2));
System.out.println("");
System.out.print("Array 3 : ");
System.out.println(String.join(",",arr3));
System.out.println("");
}
But i need to do this in a loop (without using so much print methods). I tried below way, but it only print out
Array 1 : arr1
Array 2 : arr2
likewise, it doesn't join the list that passed to the parameter.
private static void joinArrays(String[] arr1, String[] arr2, String[] arr3){
for(int i = 0; i<3;i ){
System.out.print("Array " i " : ");
System.out.println(String.join(",","arr" i));
System.out.println("");
}
}
How can i do this looping thing easy.
CodePudding user response:
There is no (easy) way to iterate through the arguments passed to a method in Java.
If you want to prevent repeating yourself, then you can put the code in a separate method, and call that with the argument variables one at a time:
private static void joinArrays(String[] arr1, String[] arr2, String[] arr3) {
joinArray("Array 1 : ", arr1);
joinArray("Array 2 : ", arr2);
joinArray("Array 3 : ", arr3);
}
private static void joinArray(String label, String[] arr) {
System.out.print(label);
System.out.println(String.join(",", arr));
System.out.println("");
}
CodePudding user response:
Just another way to do this where the parameter of the method is VarArgs. Any number of arrays (within reason) can be passed to the method. This is very similar to how command line arguments are passed to the application's main()
method except here each supplied element of the VarArgs is comma (,) delimited. A typical method call would be:
joinArrays(arr1, arr2, arr3, arr4, arr5, and so on);
If you prefer, you could pass all your arrays within a single 2D Array since a 2D Array is an Array of Arrays. An example of this would be:
String[][] array2D = new String[3][];
int arrLength = 6;
String[] arr1 = new String[arrLength];
String[] arr2 = new String[arrLength];
String[] arr3 = new String[arrLength];
array2D[0] = arr1;
array2D[1] = arr2;
array2D[2] = arr3;
joinArrays(array2D);
The end result to the console window will be exactly the same which is:
Array 1:
null, null, null, null, null, null
Array 2:
null, null, null, null, null, null
Array 3:
null, null, null, null, null, null
Of course the elements in all Arrays will all be null because there was never anything put into those arrays before they were passed to this method.
Here is the method. Read the comments in code. If you remove the comments within the method you will find it's really only three lines of code:
private static void joinArrays(String[]... arrays) {
/* Declare a variable and initialize it to hold
the System's Line Separator character sequence. */
String ls = System.lineSeparator();
/* Iterate through all supplied arrays however
many there are...it doesn't matter). */
for (int i = 0; i < arrays.length; i ) {
/* Join the elements of the current array being
processed and print the result of the Joining. */
System.out.print("Array " (i 1) ": " ls
String.join(", ",arrays[i]) ls ls);
}
}