Multi Dimensional Arrays

Matt Perley
3 min readJul 30, 2020

--

Welcome back! In today’s article I am going to explain a bit about multi-dimension arrays. They’re really not hard to understand and provide some cool functionality and knowledge that comes in handy. This article will use C# and .Net Core 2.x+.

Lets start with initializing a 2D double array.

Array init.

As you can see its as easy as providing some dimensions, just like a normal single dimension array. To visualize what’s going on you can basically just imagine the following:

Example of 2d array.

As you can see, a multi dimensional array consists of equal rows and columns. Each vector in the array holds some value. Iterating through something like this is also super easy! Lets take a look:

To explain a bit, I am using the arrays Length to iterate up and down (array.Length)and then getting the length of dimension 0. This way we can iterate side to side. (array.GetLength(x)).

Ready to add some values?

Again as you can see its not to scary. We start by getting the xx and yy values. I added one to each as the array starts at 0. For xx I added another parameter “newColumns” this specifies the number of new columns to add. You could do more with that last for loop. It could be wrapped in another loop to add multiple new columns to the data set.

There is much more I could talk about, but I wanted to give a simple over view for today. As you can see these arrays are easy to work with and super useful. I’ve used 2D arrays pretty often in game development and even more for AI ML projects. I hope you will see the value more complex arrays bring to the table.

That will do it for me today. Not really an AI/ML post, but hey, it’s related! Hopefully this will help someone out down the line.

Take care and have and excellent morning, afternoon, evening or where ever you are in your day! Until next time, happy coding!

--

--

Matt Perley
Matt Perley

Written by Matt Perley

I could go on all day in here.

No responses yet