I would like to know how to return two multidimentional arrays from the same method:
public static arraysReturn() {
int [][] A={{1,2},{2,3},{4,5}};
int [][] B={{1,2},{2,3},{4,5}};
return A,B;
}
Java does not support returning multiple things at once.
However, you could create a small class that does this:
public class TwoArrays {
public final int[][] A;
public final int[][] B;
public TwoArrays(int[][] A, int[][] B) {
this.A = A;
this.B = B;
}
}
Then make your method like this:
public static TwoArrays arraysreturn() {
int [][] A={{1,2},{2,3},{4,5}};
int [][] B={{1,2},{2,3},{4,5}};
return new TwoArrays(A,B);
}
To access values:
TwoArrays arrays = arraysreturn();
System.out.println(Arrays.toString(arrays.A)); //Due to the way java prints arrays, this is needed, but it isn't a requirement for doing other stuff with the array.
System.out.println(Arrays.toString(arrays.B));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With