Syntax help with 2D array

I’m not convinced that you can use the { ... } initializer syntax directly within that function as you do. Looking into that article @glv posted should clear things up. Alternatively, you could assign each note array to it’s respective chord individually. Something like this:

chordBank2[0] = new byte[] {40, 45, 50, 55, 59, 64}; // open strings

And then repeat for all other chords. I feel this also keeps it somewhat cleaner and more structured.

Another big issue that jumps out is the whole “arrays start indexing at 0” thing that keeps tripping people up. You seem to be aware of this, though I believe you overcorrected in this particular case.

As I understand it, you want 8 chords, with 6 notes each. These numbers represent the lenghts of the arrays you want to use. When you initialise arrays, you have to think in length, not indexes. Hence, your initialization chordBank2 = new byte[7][5]; actually creates a 2D array of lengths 7 and 5. The whole thing flips when you want to access an array. Here you need to think with “starts at 0”.

Example:
A 1D array of length 8 would be initialized with exampleArray = new int[8].
But if you want to access the last entry in that array, you use exampleArray[7].

2 Likes