In Java, is it possible to initialize several arrays in one line with the same values?
For example, consider this chunk of code
double[][] array1 = {
{ 4, 5, 1, 3},
{ 5, 6, 3, 4},
{10,-1, 45,3},
{ 1, 3, 2, 4}
};
double[][] array2 = {
{ 4, 5, 1, 3},
{ 5, 6, 3, 4},
{10,-1, 45,3},
{ 1, 3, 2, 4}
}
As you can see, both arrays are identical, and they got the same initialization. I wonder if I can declare and assign the same values to both in just one instruction.
I tried:
double[][] array1, array2 = {
{ 4, 5, 1, 3},
{ 5, 6, 3, 4},
{10,-1, 45,3},
{ 1, 3, 2, 4}
};
But in the case above, only array2
is initialized.
EDIT: I am looking for independent initialization. The solutions proposed in the possible duplicates questions do not address what I am looking for:
In the case of "Initializing multiple variables to the same value in Java", the initializations are for Strings, and there, each String has its own initialization (empty string in each case).
In the other possible duplicate "How to deep copy 2 dimensional array (different row sizes)", it involves an iterative solution, which I already knew, but I am not looking for an iterative solution