[SOLVED] 2d array random value

Hi,

Question here about 2D-arrays.
I’m running a bit ahead here in the course and made a 2D-array for passwords.
My code is running just fine because the second part of the array is always 5 (5 possible passwords).
But I think the code might crash if I have a different amount of passwords for each level.

In Java you can get the length of the “sub-array” by doing array[0].length().
How does that work in C#.

Here’s a picture of my code to assign a password.

2d_array

Hi Sjpapuuft,

The last index of an array in C# is n - 1, where n is the number of elements. With Length, you get the number of elements.

Did this help?


See also:

Thanks for responding Nina, but my question isn’t answered yet.
I’ll try to illustrate.

The code I shared is for 2D-array.
This is an array of arrays.
Consider the following picture where green is the main array.
Each blue square is an element of that array, which in itself is an array.
And red is the elements of the second (blue) array.

2D-array even

My code works fine on this example because every second array (blue) has the same amount of elements (red).
But what do I do if not every second array (blue) has the same amount of elements (red).

2D-array not even

How do I get the length of a specific blue array?

I hope this specified my question better.

You could use a jagged array plus what I said about the last index.

Thank you Nina.
You helped me where to find the answer.
I declared it wrongly I think:

I made a new array for testing purposes and declared it like this:

2d declaration

Then you can loop as you described:

2d loop

Thanks a lot!
Consider the question answered. :slight_smile:

Good job! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms