# Some problems with 2d array

**URL:** <https://discourse.processing.org/t/some-problems-with-2d-array/27109>\
**Category:** Coding Questions\
**Created:** [January 16, 2021, 9:21am UTC](https://discourse.processing.org/t/some-problems-with-2d-array/27109 "2021-01-16T09:21:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![deiwy](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@deiwy](https://discourse.processing.org/u/deiwy)\
**Post date:** [January 16, 2021, 9:21am UTC](https://discourse.processing.org/t/some-problems-with-2d-array/27109/1 "2021-01-16T09:21:22Z")

</div>

i create 2d array of map, to create a platformer game, and map is not render correctly

```auto
platform pl;

FloatList plx, ply;

int[][] map = {
  {0, 1, 1, 0}, 
  {0, 0, 0, 1}, 
  {0, 1, 1, 0}, 
  {0, 0, 0, 1}
};

void setup() {
  plx = new FloatList();
  ply = new FloatList();
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j ++) {
      if (map[j][i] == 1) {
        plx.append(i*100);
        ply.append(j*50);
      }
    }
  }

  size(400, 400);
  for (int i = 0; i < plx.size(); i++)
    pl = new platform(100, 50);
  print(plx, ply);
}

void draw() {
  rectMode(CENTER);
  background(200);
  for (int i = 0; i < plx.size(); i++) {
    for (int j = 0; j < ply.size(); j++) {
      pl.show(plx.get(i), ply.get(j));
    }
  }
}

class platform {
  float Width;
  float Height;

  platform(float w, float h) {
    Width = w;
    Height = h;
  }

  void show(float x, float y) {
    fill(40, 40, 150);
    rect(x, y, Width, Height);
    noFill();
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 16, 2021, 10:22am UTC](https://discourse.processing.org/t/some-problems-with-2d-array/27109/2 "2021-01-16T10:22:51Z")

</div>

What do you see now and what do you want to see instead?

---

<div class="post-metadata">

**Author:** ![deiwy](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@deiwy](https://discourse.processing.org/u/deiwy)\
**Post date:** [January 16, 2021, 10:24am UTC](https://discourse.processing.org/t/some-problems-with-2d-array/27109/3 "2021-01-16T10:24:45Z")

</div>

@Chrisir  
map as in array

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 16, 2021, 1:37pm UTC](https://discourse.processing.org/t/some-problems-with-2d-array/27109/4 "2021-01-16T13:37:32Z")

</div>

**Step 1**

you don’t need a nested (double) for loop in draw(), because you have all x and y data in the FloatLists. They are 1D arrays (not 2D arrays) so you can use them with a simple for-loop:

```auto
platform pl;

FloatList plx, ply;

int[][] map = {
  {0, 1, 1, 0}, 
  {0, 0, 0, 1}, 
  {0, 1, 1, 0}, 
  {0, 0, 0, 1}
};

void setup() {

  size(1400, 400);

  plx = new FloatList();
  ply = new FloatList();

  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j ++) {
      if (map[j][i] == 1) {
        plx.append(i*100+50); // adding + 50 here 
        ply.append(j*50+50); // adding + 50 here
      }
    }
  }

  for (int i = 0; i < plx.size(); i++) {
    pl = new platform(100, 50);
  }

  print(plx, ply);
}

void draw() {
  rectMode(CENTER);
  background(200);
  for (int i = 0; i < plx.size(); i++) { // ONE simple for-loop, not a nested for-loop
    pl.show(plx.get(i), ply.get(i));
  }
}

// ===================================================================

class platform {

  float Width;
  float Height;

  platform(float w, float h) {
    Width = w;
    Height = h;
  }

  void show(float x, float y) {
    fill(40, 40, 150);
    rect(x, y, Width, Height);
    noFill();
  }
}
//

```

**Step 2**

Generally speaking you make this too complicate.

The goal of having a Platform class is that you make a list of the platforms (ArrayList) and  
store the data of position (x and y) also INSIDE the class.  
Then you don’t need the two Floatlists at all.

```auto
// we make a list platforms and tell it that it contains objects of type Platform
ArrayList<Platform> platforms = new ArrayList();

void setup() {

  size(1400, 400);

  int[][] map = {
    {0, 1, 1, 0}, 
    {0, 0, 0, 1}, 
    {0, 1, 1, 0}, 
    {0, 0, 0, 1}
  };

  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j ++) {
      if (map[j][i] == 1) {
        Platform pl = new Platform(i*100+50, j*50+50, 100, 50);
        platforms.add(pl);
      }//if
    }//for
  }//for
}

void draw() {
  background(200);

  for (int i = 0; i < platforms.size(); i++) { // ONE simple for-loop, not a nested for-loop
    platforms.get(i).show();
  }
}

// ===================================================================

class Platform {

  float x; 
  float y; 
  float Width;
  float Height;

  Platform(float x_, float y_, 
    float w, float h) {
    x=x_;
    y=y_;
    Width = w;
    Height = h;
  }

  void show() {
    fill(40, 40, 150);
    rectMode(CENTER);
    rect(x, y, Width, Height);
    noFill();
  }
}//class
//

```

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![deiwy](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@deiwy](https://discourse.processing.org/u/deiwy)\
**Post date:** [January 16, 2021, 2:01pm UTC](https://discourse.processing.org/t/some-problems-with-2d-array/27109/5 "2021-01-16T14:01:08Z")

</div>

@Chrisir  
thanks its works
