Syntax help with 2D array

If that’s data that won’t change during the course of your sketch, you can simply declare it & initialize it at 1 single statement; no need for a separate function:

static final byte[][] CHORD_BANK2 = {
  { 40, 45, 50, 55, 59, 64 }, // open strings 0
  { 40, 47, 52, 56, 59, 64 }, // E major      1
  { 41, 48, 53, 57, 60, 65 }, // F major      2                   
  { 43, 47, 50, 55, 59, 67 }, // G major      3
  { 33, 45, 52, 57, 61, 64 }, // A major      4
  { 35, 47, 54, 59, 63, 66 }, // B major      5
  { 36, 48, 52, 55, 60, 64 }, // C major      6
  { 38, 38, 50, 57, 62, 66 }  // D major      7
};

The variable name CHORD_BANK2 is using the SCREAMING_SNAKE_CASE convention, hinting that its content shouldn’t ever be mutated.

P.S.: If you happen to have multiple chord banks, you can define a 3d-array instead for all of them:

static final byte[][][] CHORD_BANKS = {
  { // bank 0
    { 40, 45, 50, 55, 59, 64 }, // open strings 0
    { 40, 47, 52, 56, 59, 64 }, // E major      1
    { 41, 48, 53, 57, 60, 65 }, // F major      2                   
    { 43, 47, 50, 55, 59, 67 }, // G major      3
    { 33, 45, 52, 57, 61, 64 }, // A major      4
    { 35, 47, 54, 59, 63, 66 }, // B major      5
    { 36, 48, 52, 55, 60, 64 }, // C major      6
    { 38, 38, 50, 57, 62, 66 }  // D major      7
  },

  { // bank 1
    { 40, 45, 50, 55, 59, 64 }, // open strings 0
    { 40, 47, 52, 56, 59, 64 }, // E major      1
    { 41, 48, 53, 57, 60, 65 }, // F major      2                   
    { 43, 47, 50, 55, 59, 67 }, // G major      3
    { 33, 45, 52, 57, 61, 64 }, // A major      4
    { 35, 47, 54, 59, 63, 66 }, // B major      5
    { 36, 48, 52, 55, 60, 64 }, // C major      6
    { 38, 38, 50, 57, 62, 66 }  // D major      7
  },

  { // bank 2
    { 40, 45, 50, 55, 59, 64 }, // open strings 0
    { 40, 47, 52, 56, 59, 64 }, // E major      1
    { 41, 48, 53, 57, 60, 65 }, // F major      2                   
    { 43, 47, 50, 55, 59, 67 }, // G major      3
    { 33, 45, 52, 57, 61, 64 }, // A major      4
    { 35, 47, 54, 59, 63, 66 }, // B major      5
    { 36, 48, 52, 55, 60, 64 }, // C major      6
    { 38, 38, 50, 57, 62, 66 }  // D major      7
  }
};
1 Like