how to create array of arrays?

zixzix
edited August 2015 in How To...

i have two arrays:

color[][] colors1={{color(255,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),}, {color(0,0,0),color(0,0,0),color(255,0,0),color(255,0,0),color(255,0,0),color(0,0,0),color(0,0,0),}, {color(0,0,0),color(0,0,0),color(255,0,0),color(0,0,0),color(255,0,0),color(0,0,0),color(0,0,0),}, {color(0,0,0),color(0,0,0),color(255,0,0),color(255,0,0),color(255,0,0),color(0,0,0),color(0,0,0),}, {color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),}};

color[][] colors2={{color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),}, {color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),}, {color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),}, {color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),}, {color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),color(0,0,0),}};

i want to create a array named colors. colors[0]=colors1; colors[1]=colors2. how to do it?

Tagged:

Answers

  • edited August 2015
    color[][][] colors = new color[2][][];
    
    colors[0] = colors1;
    colors[1] = colors2;
    

    Take notice we can replace color(0, 0, 0) w/ 0.
    And color(255, 0, 0) w/ #FF0000.

    You can also store them more descriptively as constants:

    static final color BLACK = 0, RED = #FF0000;
    final color[][] colors1 = { {RED, BLACK}, {BLACK, RED, BLACK} };
    
  • thank you.it is more clearly.But how to make make a array of color[][]?

  • Answer ✓

    an array of color[][]

    is a 3D array and @GoToLoop has shown you how to do that but you rejected his answer. If the answer is not what you are looking for then you need to make you question clearer.

  • i am sorry @GoToLoop . i am not so good an English.

  • edited September 2015 Answer ✓

    you wrote:

    how to make make a array of color[][]?

    well : color[][] is a grid - it has 2 dimensions.

    it's a grid like a chess board and each cell can have a unique color (and you say E4, H7 or 5,4 or 8,7 to reach a cell/color: color[5][4])

    an array of color[][] is

    • either a chessboard that changes the colors over time or

    • it is a stack of chessboards - like 8 chessboards on top of each other. Then you say 5,4 or 8,7 on stack 6 or 7: 5,4,6 or 5,4,7 (color[5][4][6])

    to achieve this, you can also make an array of the class A

    and make this class A so that it contains color[][] colors1 and color[][] colors2

    class A would be your chessboard

    but this works of course:

    color[][][] colors = new color[2][][];
    
    colors[0] = colors1;
    colors[1] = colors2;
    

    ;-)

  • Answer ✓
    // to do
    // i want to create a array named colors. colors[0]=colors1; colors[1]=colors2. how to do it?
    // not done
    
    final color RED = color(255, 0, 0); 
    final color BLACK = color(0); 
    // final color WHITE = color(255); 
    
    
    color[][] colors1= {
      {
        RED, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
      }
      , {
        BLACK, BLACK, RED, RED, RED, BLACK, BLACK
      }
      , {
        BLACK, BLACK, RED, BLACK, RED, BLACK, BLACK
      }
      , {
        BLACK, BLACK, RED, RED, RED, BLACK, BLACK
      }
      , {
        BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
      }
    };
    
    color[][] colors2= {
      {
        BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
      }
      , {
        BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
      }
      , {
        BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
      }
      , {
        BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
      }
      , {
        BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
      }
    };
    
    void setup() {
      size(660, 660);
      //
      strokeWeight(.2);
      showAGrid(colors1, 28);
      showAGrid(colors2, 228);
    }
    
    void draw() {
      //
    }
    
    //------------------------------------------------
    
    void showAGrid(color[][] colorsLocal, int xOffSet) {
      // nested for loop
      // size
      int sizeCell = 19;
      for (int y=0; y < colorsLocal[1].length; y++) { 
        for (int x=0; x < colorsLocal.length; x++) 
        {
          fill(colorsLocal[x][y]);
          stroke(255);
          rect(x*sizeCell+xOffSet, y*sizeCell+18, sizeCell-1, sizeCell-1);
        }
      }
    }
    //
    
  • Answer ✓

    to your error:

    these

      colors[0] = colors1;
      colors[1] = colors2;
    

    belong in setup()

  • edited September 2015 Answer ✓

    or like this

    // 
    // i want to create a array named colors. colors[0]=colors1; colors[1]=colors2. how to do it?
    // done. 
    
    // global vars 
    color[][][] colors = new color[2][][];
    
    //------------------------------------------------
    // MAIN functions  
    
    void setup() {
      // init 
      size(660, 660);
      //
      define();
      //
      strokeWeight(.2);
      showAGrid(colors[0], 28);
      showAGrid(colors[1], 228);
      strokeWeight(1);
    }
    
    void draw() {
      // empty
    }
    
    //------------------------------------------------
    // Tools 
    
    void define() {
    
      // local vars here 
      // 
      final color RED = color(255, 0, 0); 
      final color BLACK = color(0); 
      // final color WHITE = color(255); 
    
    
      color[][] colors1= {
        {
          RED, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
        }
        , {
          BLACK, BLACK, RED, RED, RED, BLACK, BLACK
        }
        , {
          BLACK, BLACK, RED, BLACK, RED, BLACK, BLACK
        }
        , {
          BLACK, BLACK, RED, RED, RED, BLACK, BLACK
        }
        , {
          BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
        }
      };
    
      color[][] colors2= {
        {
          BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
        }
        , {
          BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
        }
        , {
          BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
        }
        , {
          BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
        }
        , {
          BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK
        }
      };
    
      // global var 
      colors[0] = colors1;
      colors[1] = colors2;
    }
    
    //------------------------------------------------
    
    void showAGrid(color[][] colorsLocal, int xOffSet) {
      // nested for loop
      // size
      int sizeCell = 19;
      for (int y=0; y < colorsLocal[1].length; y++) { 
        for (int x=0; x < colorsLocal.length; x++) {
          fill(colorsLocal[x][y]);
          stroke(255);
          rect(x*sizeCell+xOffSet, y*sizeCell+18, sizeCell-1, sizeCell-1);
        }
      }
    }
    
    //------------------------------------------------
    //
    
  • thank you so much!

  • Answer ✓

    great

    ;-)

  • edited September 2015 Answer ✓

    interestingly, you had a comma at the end before } each time

    color(0,0,0),color(0,0,0),} 
    

    and it did work... iirc

    I deleted the commas there (all before } )

    but I still wonder what's going on there... an empty element...?

  • Answer ✓

    I didn't test it really

  • i test .have comma or no comma all works fine

  • Java doesn't mind about 1 extra trailing comma. Neither JavaScript! ;;)
    And it's useful when we decide to add more elements later on. :-bd

  • Answer ✓

    ;-)

Sign In or Register to comment.