Arrays in a Array

edited February 2014 in Programming Questions

Hello Community,

I work on a project where I need many arrays,and my questions are

1) Is it possible to store arrays of the same type (all integer arrays) in on array,something like

blockcontrol_alpha[]={blockcontrol_1,blockcontrol_2,blockcontrol_3....etc.}

In this case,blockcontrol_1 etc. are two dimensional arrays. I search for something like this because I need a performance saving way to store lots of data and I dont need more than 10 arrays at the same time for working. My game is divided in areas of 240x360 px and I only have to load the arrays you are possible to see.

2) Is there a way to declare new arrays while the game is running? At the beginning there are 60 integer arrays with a defined number of 150 fields for each array. At the moment where the player would came closer to the end of the map I have to declare new arrays.

Maybe are my questions are little bit confusing but I hope you understand what I mean.

greetz Flozzel

Tagged:

Answers

  • You can also use 3D arrays to hold the data

    Don't think you can declare them later but you could overwrite the old data

    You can increase mem in the prefs...

    Did you have difficulties? Try load everything in setup() first.

  • edited February 2014 Answer ✓

    Hi Flozzel,

    yes arrays can have any dimension (usually 3 dimensions is the maximum you need, else you should think of a different datastructure) so this is an example of 2dimensional arrays:

    //as usual define the array 
    int[][] matrix = new int[4][4];
    
    // define a 2nd 2dim array. 4 elements in the first dimension, but 2nd dimension
    // is flexible and each array there has to be defined later.
    int[][] matrix2 = new int[4][];
    // e.g
    matrix2[0] = new int[10];
    // or
    matrix2[1] = new int[] {1,2,3,4,5};
    

    hope that helps

  • Thanks this is very helpfull. Maybe this is a way to get my problem solved. I have to rethink my way of programming,dont think its the right way like that.

    greetz Flozzel

  • Edit: This is a little bit confusing :)

    Can you give me an example for an two dimensional array with 5 fields? For example this values would be stored in one array matrix:

    blockcontrol_1[0][0]=240; blockcontrol_1[0][1]=360; blockcontrol_1[0][2]=2; blockcontrol_1[0][3]=1; blockcontrol_1[0][4]=0;

    blockcontrol_1[1][0]=264; blockcontrol_1[1][1]=360; blockcontrol_1[1][2]=3; blockcontrol_1[1][3]=1; blockcontrol_1[1][4]=0;

    blockcontrol_1[2][0]=1200; blockcontrol_1[2][1]=360; blockcontrol_1[2][2]=2; blockcontrol_1[2][3]=1; blockcontrol_1[2][4]=0;

    blockcontrol_1[3][0]=1224; blockcontrol_1[3][1]=360; blockcontrol_1[3][2]=3; blockcontrol_1[3][3]=1; blockcontrol_1[3][4]=0;

    ......

    What would be my [x][y] for getting blockcontrol[2][1] in the case of an array like int[][] matrix = new int[4][4]; ?

    greetz Flozzel

  • I don't get your question:

    What would be my [x][y] for getting blockcontrol[2][1] in the case of an array like int[][] matrix = new int[4][4]; ?

    it would be matrix[2][1] - but what do you mean?

    blockcontrol and matrix are both 2D arrays.

    A 2D Array is like chessboard: you tell the row number and the column number and come to a cell with a value / number.

    You can have a grid 5x5 or like chess 8x8 or 100x1000 - it stays 2D.

    1D array is a simple list (only row numbers)

    3D array - think of 8 boards of chess stacked one upon another - you tell the etage number, the row number and the column number and come to a cell with a value / number.

    useful for a game like this:

    http://www.tischlereihein.de/Bilder/Brettspiele/Anleitung/3D-Muehle.gif

  • this is a typical 2D array

    Rect[][] rekt = new Rect[7][7];
    
    void setup() {
      size(800, 800);
    
      for (int i = 0; i < rekt.length; i++) {
        for (int j = 0; j < rekt[i].length; j++) {
          rekt[i][ j] = new Rect( 58 + i*(width/10), 58 + j*(height/10));
        }
      }
      //
    } // func 
    
    void draw() {
    
      fill(255, 5);
      noStroke();
      rect(0, 0, width*2, height*2);
    
      for (int i = 0; i < rekt.length; i++) {
        for (int j = 0; j < rekt[i].length; j++) {
          rekt[i][ j].show();
        }
      }
      //
    } // func 
    
    // =====================================================
    
    class Rect {
    
      float x;
      float y;
    
      Rect(float x_, float y_) {
        x = x_;
        y = y_;
      } // constr
    
      void show() {
        rectMode(CENTER);
        stroke(39, 20, 1, 150);
        noFill();
        rect(x, y, 55, 55, 7);
      } // method
    } // class 
    //
    
  • Oh yes,of course I mean a 3d array.But I understanded it now,

    "3D array - think of 8 boards of chess stacked one upon another - you tell the etage number, the row number and the column number and come to a cell with a value / number."

    is an good example thanks. greetz Flozzel

  • edited February 2014

    this is a simple 3D array

    • the array holds rects/cubes of class Rect

    • it features a rotating camera

      // cubes 
      Rect[][][] rekt = new Rect[7][7][7];
      
      // cam 
      PVector camPos;     // its vectors 
      PVector camLookAt;
      PVector camUp;
      // cam rotation 
      float camCurrentAngle;       // for cam rot around center
      float camRadius;             // same situation 
      
      void setup() {
        size(800, 800, OPENGL);
      
        // set vectors 
        camPos    = new PVector(width/2.0, height/2.0, 600);
        camLookAt = new PVector(width/2.0, height/2.0, -300);
        camUp     = new PVector( 0, 1, 0 );
      
        defineCubes();
      
        background(111);
      
        println ("Any key to reset.");
        //
      } // func 
      
      void draw() {
        background(111);
      
        camera (camPos.x, camPos.y, camPos.z, 
        camLookAt.x, camLookAt.y, camLookAt.z, 
        camUp.x, camUp.y, camUp.z);
      
        lights();
      
        for (int i = 0; i < rekt.length; i++) {
          for (int j = 0; j < rekt[i].length; j++) {
            for (int k = 0; k < rekt[i][j].length; k++) {
              rekt[i][ j][k].show();
            }
          }
        }
        // cam 
        camCurrentAngle++;
        lookAtAngle();
        //
      } // func 
      
      void keyPressed () {
        defineCubes();
      }
      
      // ----------------------------------------------------
      
      void defineCubes() {
      
        // define cubes
        for (int i = 0; i < rekt.length; i++) {
          for (int j = 0; j < rekt[i].length; j++) {
            for (int k = 0; k < rekt[i][j].length; k++) {
              // prepare values 
              color currentCol = color (random(255), random(255), random(255));
              boolean exist;
              // the percentage 
              if (random(100) > 60) 
                exist = true;
              else
                exist = false;  
              // create a cube   
              rekt[i][ j][k] = new Rect( 158 + i*(height/10), 
              158 + j*(height/10), 
              - k*(height/10), 
              currentCol, 
              exist);
            }
          }
        }
      }
      void lookAtAngle() {
        // rot in the plane 
        camRadius = camLookAt.dist (camPos); 
        camPos.x = camRadius * cos (radians(camCurrentAngle)) + camLookAt.x;
        camPos.z = camRadius * sin (radians(camCurrentAngle)) + camLookAt.z;
      } // func 
      
      // =====================================================
      
      class Rect {
      
        float x;
        float y;
        float z;
        color col;
        boolean exist = true; 
      
        Rect(float x_, float y_, float z_, 
        color col_, 
        boolean exist_ ) {
          x = x_;
          y = y_;
          z = z_;
          col = col_;
          exist = exist_ ;
        } // constr
      
        void show() {
          if (exist) {
            // rectMode(CENTER);
            // stroke(39, 20, 1, 150);
            noStroke();
            //noFill();
            //fill(244, 1, 1);
            fill(col);
            // rect(x, y, 55, 55, 7);
            pushMatrix();
            translate(x, y, z);
            // rotateX(radians(30));
            box(55);
            popMatrix();
          } // if
        } // method
      } // class 
      //
      
Sign In or Register to comment.