-3

Java beginner here. I have 2 string arrays A, B. for a new empty array C, I'd like to assign each value in A B to C. Code I've got as following:

String[] A = {"banana", "orange", "apple"};
String[] B = {"2", "3"};
String[][] C;

private String[][] mix() {
  for (int i = 0; i < this.A.length; i++) {
    for (int j = 0; j < this.B.length; j++) {
        C[i][j] = {A[i], B[j]};
    }
}

The above code gives me error on C[i][j] = {A[i], B[j]};:

Array constants can only be used in initializers

EDIT: my expected output would be

C = {{"banana", "2"}, {"orange", "2"}, {"apple", "2"}, {"banana", "3"}, {"orange", "3"}, {"apple", "3"}}

or any permutation like above with all 6 combinations.

one-hand-octopus
  • 2,229
  • 1
  • 17
  • 51
  • 3
    Dang, I'm too tired, While `C` is a 2D array, and `C[i]` is a 1D array, `C[i][j]` is but a String, not an array. You can't declare it equal to an array of any type. – Hovercraft Full Of Eels Feb 11 '22 at 00:32
  • 3
    Can you provide the expected output of `C`? – Roger Ng Feb 11 '22 at 00:33
  • 2
    Do you just want `String[][] C = {A, B}`, or do you want `{"banana", "2"}, {"orange", "3"}`? – OneCricketeer Feb 11 '22 at 00:40
  • 1
    As mentioned by HovercraftFullOfEels, this will not work `C[i][j] = {A[i], B[j]};` but this will work `C[i] = new String[]{A[i], B[j]};`, however, I don't think you want to use a 3D array like this. Can you show us what you expect the output to look like? A hashmap with the fruit as the key, and the number as the value migth be a better option here. – sorifiend Feb 11 '22 at 00:52
  • @HovercraftFullOfEels each element of `C` should be an array like this: `{"banana", "2"}` – one-hand-octopus Feb 11 '22 at 12:04
  • @sorifiend for this case there is no key value pair here, so I think each element of `C` to be something like `{"banana", "2"}` would be the result – one-hand-octopus Feb 11 '22 at 12:05

1 Answers1

0

As pointed out in comments you need to use C[i] = ... to assign a 2D array to a 3D array, and you need to use new String[]{...} to correctly create a 2D array like so C[i] = new String[]{A[i], B[j]};.

You can only use C[i][j] = ...; if you assign a string like so C[i][j] = "apple";

Here is a working example with the format you are looking for. Note how we use the logic above, and how we have swapped around the B and A for loops to get the correct output:

String[] A = {"banana", "orange", "apple"};
String[] B = {"2", "3"};
String[][] C;

private String[][] mix() {
    //We need to initialize the array before we place items into it
    //We know the size because you want every fruit to show up for every number so we use "B.length"
    C = new String[B.length][];

    //Use B in the outer loop because so that 2 comes out first, then 3
    for (int i = 0; i < this.B.length; i++) {

        //Use A in the inner loop to get the pattern you want
        for (int j = 0; j < this.A.length; j++) {

            //Assign a new 2D child array to the 3D array "C"
            C[i] = new String[]{this.A[j], this.B[i]};
        }
    }
}

Now if you loop through the C array it will produce exactly your result:

{{"banana", "2"}, {"orange", "2"}, {"apple", "2"}, {"banana", "3"}, {"orange", "3"}, {"apple", "3"}}
sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • How come C has length of 6 while it was initialized with `C = new String[B.length][];`? – one-hand-octopus Feb 12 '22 at 11:34
  • 1
    @thinkvantagedu Because that is how a 3D array works. The 3D array contains 6 different 2D arrays which each contain a two Strings (a fruit and a number), for a total of 12 individual strings, exactly like you showed in your question. – sorifiend Feb 12 '22 at 14:07