# Having a problem with zero length arrays

**URL:** <https://discourse.processing.org/t/having-a-problem-with-zero-length-arrays/45555>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [January 8, 2025, 5:45am UTC](https://discourse.processing.org/t/having-a-problem-with-zero-length-arrays/45555 "2025-01-08T05:45:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tajvirsingh](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tajvirsingh](https://discourse.processing.org/u/tajvirsingh)\
**Post date:** [January 8, 2025, 5:45am UTC](https://discourse.processing.org/t/having-a-problem-with-zero-length-arrays/45555/1 "2025-01-08T05:45:03Z")

</div>

![Screenshot 2025-01-08 at 2.36.16 pm](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/5/b55d165573631f4516ae49206bdd10a0ecaa79bc.jpeg)  
I’ve formatted all my vectors to be 1 column and 4 rows, but apparently I’m putting a vector of length 0 into my function? Why might this happen?  
Thanks,

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [January 8, 2025, 1:26pm UTC](https://discourse.processing.org/t/having-a-problem-with-zero-length-arrays/45555/2 "2025-01-08T13:26:36Z")

</div>

> [@tajvirsingh](#):
>
> I’ve formatted all my vectors to be 1 column and 4 rows

The problem is that you are creating and initialising a 1D array (column) of 4 zero length arrays (rows).

If you try this code it demonstrates the problem

```auto
float a[][] = {{}, {}, {}, {}};
                       // OUTPUT
println(a.length); // 4 
println(a[0].length); // 0

a[0][0] = 3.142; // ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [January 8, 2025, 1:43pm UTC](https://discourse.processing.org/t/having-a-problem-with-zero-length-arrays/45555/3 "2025-01-08T13:43:57Z")

</div>

This code works

```auto
float a[][] = {{0}, {0}, {0}, {0}};
// OUTPUT
println(a.length); // 4
println(a[0].length); // 0

a[0][0] = 3.142;
println(a[0][0]); // 3.142

```

---

<div class="post-metadata">

**Author:** ![tajvirsingh](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tajvirsingh](https://discourse.processing.org/u/tajvirsingh)\
**Post date:** [January 9, 2025, 1:25am UTC](https://discourse.processing.org/t/having-a-problem-with-zero-length-arrays/45555/4 "2025-01-09T01:25:14Z")

</div>

Thank you for your response.

I am trying develop this code to render 3D objects. It is still not helping. Do you want to send you whole code and you look at it.

Here is the code.  
int scr\_width = 1200;  
int scr\_height = 800;  
int x\_coord = 0;  
int y\_coord = 0;  
int z\_coord = 0;  
int cube = new int[8];  
int cube\_list = {{1, 2, 3, 4}, {1, 2, 6, 5}, {1, 4, 8, 5}, {2, 3, 7, 6}, {3, 4, 8, 7}, {5, 6, 7, 8}};  
int cube\_portray = new int[8][2];  
int center = {50, 50, 50};  
int cube\_radius = 20;  
float x\_angle = 0;  
float y\_angle = 0;  
float z\_angle = 0;  
float fov = PI/2;  
float aspect = scr\_width/scr\_height;  
float near = 0;  
float far = 0;  
float speed = 1;  
float cube\_corner(){  
float c = new float[8][3][1];  
int f = 0;  
for(int i = -1; i \< 2; i += 2){  
for(int j = -1; j \< 2; j += 2){  
for(int k = -1; k \< 2; k += 2){  
c[f][0][0] = (float)center[0]+cube\_radius_i;  
c[f][1][0] = (float)center[1]+cube\_radius_j;  
c[f][2][0] = (float)center[2]+cube\_radius_k;  
f += 1;  
}  
}  
}  
return c;  
}  
float[][] matmul(float[][] a, float[][] b){  
float c[][] = new float[a.length][b[0].length];  
for (int i = 0; i \< a.length; i++){  
float d[] = new float[a.length];  
for (int j = 0; j \< b[0].length; j++){  
float e = 0;  
for (int k = 0; k \< b.length; k++){  
e += a[i][k]b[k][j];  
}  
d[j] = e;  
}  
c[i] = d;  
}  
return c;  
}  
float[][] projmul(float[][] a){  
float projection[][] = {{1/(aspecttan(fov/2)), 0, 0, 0}, {0, 1/(tan(fov/2)), 0, 0}, {}, {}};  
float x\_rot[][] = {{1, 0, 0, 0}, {0, cos(x\_angle), -sin(x\_angle), 0}, {0, sin(x\_angle), cos(x\_angle), 0}, {0, 0, 0, 1}};  
float y\_rot[][] = {{cos(y\_angle), 0, sin(y\_angle), 0}, {0, 1, 0, 0}, {-sin(y\_angle), 0, cos(y\_angle), 0}, {0, 0, 0, 1}};  
float z\_rot[][] = {{cos(z\_angle), -sin(z\_angle), 0, 0}, {sin(z\_angle), cos(z\_angle), 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};  
float viewport[][] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};  
float view[][] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};  
a = matmul(viewport, a);  
a = matmul(view, a);  
a = matmul(x\_rot, a);  
a = matmul(y\_rot, a);  
a = matmul(z\_rot, a);  
a = matmul(projection, a);  
return a;  
}  
void keyPressed(){  
if(key == ‘w’){  
x\_coord += speed_cos(x\_angle);  
z\_coord += speed_sin(x\_angle);  
}  
if(key == ‘a’){  
x\_coord -= speed_sin(x\_angle);  
z\_coord += speed_cos(x\_angle);  
}  
if(key == ‘s’){  
x\_coord -= speed_cos(x\_angle);  
z\_coord -= speed_sin(x\_angle);  
}  
if(key == ‘d’){  
x\_coord += speed_sin(x\_angle);  
z\_coord -= speed\*cos(x\_angle);  
}  
}  
void setup(){  
size(1200, 800);  
}  
void draw(){  
float coords\_list = cube\_corner();  
float proj\_list = new float[8][4][1];  
for(int i = 0; i \< coords\_list.length; i++){  
float vec = projmul(coords\_list[i]);  
for(int j = 0; j \< 3; j++){  
proj\_list[i][j][0] = vec[j][0];  
}  
proj\_list[i][4][0] = 1;  
}  
background(0);  
for(int i : cube\_list){  
beginShape();  
for(int j = 0; j \< 4; j++){  
vertex(proj\_list[i[j]][0][0], proj\_list[i[j]][1][0]);  
}  
endShape();  
}  
}

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [January 9, 2025, 10:18am UTC](https://discourse.processing.org/t/having-a-problem-with-zero-length-arrays/45555/5 "2025-01-09T10:18:03Z")

</div>

Welcome to the forum when you paste code it is best to [format your code](https://discourse.processing.org/t/guidelines-asking-questions/2147#is-your-post-formatted-9) to make it easier for others to read. You can edit your last post to format the code rather than re-post.

> [@tajvirsingh](#):
>
> I am trying develop this code to render 3D objects.

I see in the code you have specified the details of a 3D cube so are you planning to render

1. simple 3D geometric shapes e.g. cuboids, ellipsoids, cones, tubes etc, or
2. complex 3D models e.g. those used in games etc.

If the first option do you

1. want to create the source code from scratch, or
2. are you prepared to use a contributed library to do the hard work.

If the later then I can recommend a truly excellent library, Shapes3D it comes with good examples and supporting [website](http://www.lagers.org.uk/s3d4p/index.html). It can even be installed in the PDE using the contributions manager 😁 Sorry about the plug but as the library creator I am a little biased. 😇

If the former then I suggest you use PVector and PMatrix3D to represent and manipulate 3D coordinates rather than 2D arrays directly.

Creating and manipulating 2D arrays in Java is mind bending stuff. In the code below two arrays are declared and defined , `a` and `b` , they look similar but produce orthogonal arrays

```auto
float a[][] = new float[1][4];
float b[][] = {{0}, {0}, {0}, {0}};

void setup() {
  a[0][3] = 3.142;
  b[3][0] = 2.728;
  a2dPrint("Array 'a'", a); 
  a2dPrint("Array 'b'", b); 
}

void a2dPrint(String title, float[][] array) {
  println("######", title, "#####");
  println(" Size", array.length, "x", array[0].length);
  for (int i = 0; i < array.length; i++) {
    print(" ");
    for (int j = 0; j < array[i].length; j++) {
      print(array[i][j], " ");
    }
    println();
  }
  println("---------------------------------------");
}

```

Output:

```auto
###### Array 'a' #####
   Size 1 x 4
    0.0 0.0 0.0 3.142   
---------------------------------------
###### Array 'b' #####
   Size 4 x 1
    0.0   
    0.0   
    0.0   
    3.142   
---------------------------------------

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [January 9, 2025, 6:12pm UTC](https://discourse.processing.org/t/having-a-problem-with-zero-length-arrays/45555/6 "2025-01-09T18:12:39Z")

</div>

The following technique will print the arrays in a similar manner:

```auto
float[][] a = new float[1][4];
float b[][] = {{0}, {0}, {0}, {0}};

void setup() {
  a[0][3] = 3.142;
  b[3][0] = 2.728;
  printArray(a);
  for (int i = 0; i < a.length; i++) {
    println(a[i]);
  }
  printArray(b);
  for (int i = 0; i < b.length; i++) {
    println(b[i]);
  }
}

```

**Output:**

 ![o](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/0/809f57dde43292c152a17f0d8c315a294415a797.png)

I would like to see a screenshot of the posted code’s output. If there is a reference for the code I would like to see that also. To reiterate, the original post needs to be formatted because copy pasting leaves out some important characters.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [January 9, 2025, 9:47pm UTC](https://discourse.processing.org/t/having-a-problem-with-zero-length-arrays/45555/7 "2025-01-09T21:47:27Z")

</div>

> [@svan](#):
>
> The following technique will print the arrays in a similar manner:

True but the arrays are still orthogonal `1x4 versus 4x1` which is a problem if the user mixes and matches the two array definitions in the same sketch.
