I'm going to break this down into each line of the code, and explain what it does.
String[,] parkingLot = { { "Mustang", "F-150", "Explorer" }, { "Corvette", "Camaro", "Silverado" }, { "Corolla", "Camry", "Rav4" } };
parkingLot is a multi-dimensional string Array. This means this is a string array that hold other arrays of type string. Think of each {} within the bigger {} its own Array object that can be accessed.
for (int i = 0; i < parkingLot.GetLength(0); i++)
For loops are designed to loop through a specified number of elements. the integer "i" created at the start indicates that the loop will start at index 0. We start at 0 because array indexing starts at 0.
The duration of this loop lasts as long as the number of arrays ({}) in the initialised array. In this case, because parkingLot contains 3 arrays, the length is 3.
for (int j = 0; j < parkingLot.GetLength(1); j++)
This part of the code iterates within the for-loop above it, and iterates over every element inside a single array. The "GetLength(1)" part of the code grabs the length of a single array instance within the overall array. For example: the first array instance in this multi-dimensions array contains { "Mustang", "F-150", "Explorer" }, being 3 strings. Therefore, the length will also be 3.
Console.Write(parkingLot[i, j] + " ");
Here, we can see that there's a reference to i and j. Let me show you what happens step by step:
when i=0, the code begins to iterate another loop, which must compute j = 0, j = 1 and j = 2 before moving to i = 1. This means that it must read every element in the array before moving to the next array.
For Example: say:
- i = 0, and j = 0. this would be accessing "Mustang"
- i = 1, and j = 2 would be accessing "Silverado"
- i = 2 and j = 1 would be accessing "Camry"
Console.WriteLine();
After writing each element in the j loop, this piece of code writes the responses to the console. That's why each elements of arrays appear on one line.
Console.ReadKey();
After you press this key, the program closes.
I suggest you play around with the variables and create your own arrays and multi-dimensional arrays. That way you can see how they operate and what they don't like you doing with them.