Arrays: What you should know



This content originally appeared on DEV Community and was authored by VernocDatBom

Were you ever one of those nerdy kids who used Google Sheets to make graphs and stuff instead of creating a blocky, inaccurate mess of a bar graph in your presentation group project?

Wanna do it again?

Arrays are the most useful data structures in code for holding a lot of data. And I mean a LOT. It’s your code, you can write 10,000 lines of pure arrays if you want to.

But let’s say you have never seen an array before, and have no idea what it does. What does it look like? Unlike most people would expect, arrays are actually variables. They’re a special type of variable that can hold more than one value. A simple array would be something like;

array = [0, 6, 2, 4]

Each value in the array is one variable. Because variables can hold many data types, you could write an array like;

array2 = ["Apples", 21, emotion, "Married"]

But wait! There’s more (Dimensions)!

Of course, if you have more data than just a few variables, things can get messy. We thought of that.

There’s more to arrays than just the one dimension. Yes, the array example was a 1-Dimensional array. This means there are 2-Dimensional arrays, as well as 3, 4, 27, and so on. And no, I will not type a 27-Dimensional array for your entertainment.

array2d =
[
   [11, 12, 13, 14],
   [21, 22, 23, 24],
   [31, 32, 33, 34]
]

array3d = 
[
   [
      [111, 112, 113, 114],
      [121, 122, 123, 124],
      [131, 132, 133, 134]
   ],
   [
      [211, 212, 213, 214],
      [221, 222, 223, 224],
      [231, 232, 233, 234]
   ]
]

“But how would I ever use this?”

You may ask. in order to pull values from an array, you have to type the value of each dimension like;

array[2] // 6

array2d[2][3] // 32

array3d[2][3][1] // 132

When you would ever use these? Well, just like normal variables. Here are a few examples;

if array2d[1][3] = 999
{
   draw_text(32, 32, "I did something!")
}

if 12 = 12
{
   show_message(array3d[4][1][2])
}


This content originally appeared on DEV Community and was authored by VernocDatBom