How do I make a 2 dimensional array?

I’m watching a Coding Train video “ Coding Challenge #11: 3D Terrain Generation with Perlin Noise in Processing” and he uses float[][]; and it doesn’t work on p5.js and I was wondering if they changed it and what I can use as a alternative.

If the tutorial you are referring to is in java and you just tried to use the same code in p5.js it won’t work. Javascript and Java are fundamentely different. If you don’t want to learn a new programming language, but still program for the web using processing, I’d suggest using Processing.JS which allows you to use Java Processing in web applications.

Arrays in Java and Javascript are very different this example creates a 2D 10x10 array

var table = new Array(10);
for(var i = 0; i < table.length; i++)
  table[i] = new Array(10);
function createFloatArray2d(rows, cols) {
  return Array.from({ length: rows }, _ => new Float32Array(cols));
}
1 Like