# Processing should define Array just like it has Vector

**URL:** <https://discourse.processing.org/t/processing-should-define-array-just-like-it-has-vector/44840>\
**Category:** Development\
**Created:** [July 26, 2024, 7:25pm UTC](https://discourse.processing.org/t/processing-should-define-array-just-like-it-has-vector/44840 "2024-07-26T19:25:31Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![project\_2501](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@project\_2501](https://discourse.processing.org/u/project_2501)\
**Post date:** [July 30, 2024, 2:43pm UTC](https://discourse.processing.org/t/processing-should-define-array-just-like-it-has-vector/44840/23 "2024-07-30T14:43:01Z")

</div>

`arr(x,y)` is fine.

And better than

`arr[x][y]`

… from a beginner’s point of view, imho.

Which suggests it can be implemented just as Vector was, using round brackets.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 30, 2024, 5:38pm UTC](https://discourse.processing.org/t/processing-should-define-array-just-like-it-has-vector/44840/24 "2024-07-30T17:38:09Z")

</div>

> [@project\_2501](#):
>
> `arr(x,y)` is fine.

I usually use the name `arr` for declaring array variables! 🙄  
So something more mouthful like `makeArr2d(rows, cols)` would be safer. 😇

---

<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:** [July 30, 2024, 5:54pm UTC](https://discourse.processing.org/t/processing-should-define-array-just-like-it-has-vector/44840/25 "2024-07-30T17:54:39Z")

</div>

The advantage of using the double `[][]` brackets is clear during assignment and is a simple extension of the 1D array assignment e.g.

```auto
arr[x][y] = 3.14159; // OK works fine
println(arr[x][y]); // outputs 3.14149

```

if the 2D array is implemented using a class, like PVector then the equivalent might be thought of as

```auto
arr(x, y) = 3.14159; // illegal syntax will not compile

```

you would need something like

```auto
arr(x, y, 3.14159);

```

The actual syntax would be determined by the class implementation.

I assume that your students are using standard 1D array syntax `[]` so using `[][]` for assigning and retrieving values should be a logical step in their learning. The real difficulty is understanding how to create the array in the first place

So expanding on @GoToLoop’s idea this function will create a 2D array of floats

```auto
float[][] makeArray2D(int nbrCols, int nbrRows){
  float[][] a = new float[nbrCols][];
  for(int i = 0; i < nbrCols; i++)
    a[i] = new float[nbrRows];
  return a;
}

// and using it
float [][] array;

void setup(){
 array = makeArray2D(9,3);
}

```

so the indices are `[col_nbr][row_nbr]` which is probably what you want for graphics but it is simple enough to change the implementation to get `[row_nbr][col_nbr]` indices

---

<div class="post-metadata">

**Author:** ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)\
**Post date:** [July 30, 2024, 6:58pm UTC](https://discourse.processing.org/t/processing-should-define-array-just-like-it-has-vector/44840/26 "2024-07-30T18:58:32Z")

</div>

Nice thread, some great input.

I’ll inject an observation out from left field: I feel we’re discussing solutions although the issue at hand is not yet agreed upon. All here are giving great input, but I don’t feel we’re all taking about the same problem yet.

OP posits – based on their tutoring observations – that the current array implementation is too high a barrier for novices. But what does this actually mean?

Do beginners bounce off of the square bracket syntax? Or is the core concept of an array just a way of thinking that’s not very intuitive? Is the use of indices too opaque? Is it something else? And before everything: is there evidence that this observation is a wide-spread issue at all?

Whatever the concrete problem definition is, the solution will get pulled into different directions.

I don’t think I’m overestimating just how much novices struggle with code. From my own experiences in attending creative coding courses during my studies, I can tell you that the mere presence of “strange” characters like ; and { and & put the fear in a lot of fellow pupils, e.g. even after the course, they where struggling in finding how to type a brace. Sounds stupid, but that’s a reality.  
Everybody would eventually come around to “getting” the idea behind variables. They’d come to understand the difference between int and float. They’d be able to draw a smiley face using basic shapes. Maybe they’d get a basic if() statement running. But anything more complex would become a really really high barrier. I’m not kidding. (Although I will caveat, that this was a low-weighted course on the curriculum and motivations to perform… varied).

What I’m saying: for all the technical details that might need sussing out in this discussion, never forget to step out of your expert mode and go down to the level of full beginners and consider their immediate woes.

Alternatively…

There may also be a hard truth that coding can NOT be made universally accessible. As in, higher foundational concepts – like arrays – do require that the person puts in that effort necessary to rewire their brain into coding-mode. It’s clearly not for everyone and no amount of abstraction and simplification will ever make it easily accessible.

Uff, wall of text. Sorry for that.

---

<div class="post-metadata">

**Author:** ![project\_2501](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@project\_2501](https://discourse.processing.org/u/project_2501)\
**Post date:** [July 30, 2024, 10:50pm UTC](https://discourse.processing.org/t/processing-should-define-array-just-like-it-has-vector/44840/27 "2024-07-30T22:50:47Z")

</div>

100% @eightohnine - you are **absolutely right** to redirect us back to the core issue - accessibility to non-experts.

The reason I made this suggestion is that (1) I do care a lot about removing barriers, and (2) I have seen how those same beginners find arrays much easier in other languages.

I’d also like to redirect those interested in the technical discussion away from “square vs round brackets” back to the issue of every programmer in javascript has to “roll their own” arrays. That’s just not good in 2024.

By the way, I’m not singling out javascript. Python doesn’ have arrays - you have to use numpy.

Fortran 77 - yes, **from 1977** - does have [arrays](https://web.stanford.edu/class/me200c/tutorial_77/10_arrays.html). Eg `real A(3,5)` creates a variable A which is a 2-d array of real values with dimensions 3 by 5.

[Previous page](https://discourse.processing.org/t/processing-should-define-array-just-like-it-has-vector/44840.md?page=1)
