‫Welcome back.
‫In this video, we are going to cover Jagged Ray.
‫And you have seen how to store values inside of an array, for example, an integer inside of an array
‫or a string.
‫But what you also can do is to store an array within an array, and that's an array section.
‫So pretty much jagged array is an array with an array, and that's what we're going to create in this
‫video.
‫So let's go ahead and create one.
‫I'm going to do that locally here within a main method.
‫So I'm just going to create one with two square brackets.
‫So you need two square brackets.
‫So this is where you say, I want to have an array within an array or I want to have a jagged array.
‫And it's not just one array within array, it's multiple arrays within one array.
‫So instead of just storing an element like a value, for example, an integer, a number five or something
‫like that, within an array, you can also store an array and inside of that same spot.
‫So you can store multiple arrays within that array and we will see what that means.
‫So let's create one and call it Jagged Array and it's going to be a new int.
‫So I'm just going to create an integer array here and in here.
‫I can define how many arrays I want to have within that array.
‫So I'm just going to say, for example, I want to have three arrays within that jagged three.
‫So that's how I declare a directory, declare jagged array.
‫Now in order to add values to it, I can go ahead and say Jagged Ray at the position zero should be
‫a new int with let's say five values and I can do the same thing in the next row.
‫Checked array one and new int.
‫Let's say this one only has three values and then another jagged array.
‫And that one will be at the position too, which is a new int with two entries.
‫So now what I need to do is to add values to them.
‫So for now I have arrays.
‫But this for example, at the position zero of my directory, I have an array of five values, but there
‫are no values there yet, so they are empty yet.
‫So we can either declare them that way or assign the arrays inside of the directory like that, or we
‫can use initialize us instead so we can simply say the array at position zero is a new int and I'm not
‫specifying how many values it has, I just enter the values.
‫So let's say in the first one I said there would be five values.
‫So I just enter two, three, five, seven, 11, then reject array at the position one, I create the
‫new int and I simply add the values in here.
‫So this time three values.
‫So one, two and three.
‫And then the array at position two is a new int and this one only has two values.
‫So I'm just going to enter 13 and 21 random values doesn't really matter that much.
‫So that's generally how you can initialize eject array, how you can add values to it.
‫But there is an alternative way to return it to weight and that is to create the array and add the values
‫straight away.
‫So while declaring I initialize a directly so reject array two is going to be a new int double square
‫brackets and now I can go ahead and add the values.
‫So I'm just going to add the same values as I did here so I can simply go ahead and add them as if I
‫would simply add values to an array.
‫As you can see, I need to declare what type of value I want to have in here.
‫And then I, well, I simply create the array straightaway, so I initialize the array.
‫So this doesn't have a name, it doesn't have an own name, but it could be an own array.
‫So if I now want to access, let's say, this five here, how would I do that?
‫Well, you maybe have an idea.
‫And generally speaking, we could do that with CW.
‫So I access the console double tap, and then here the value in the middle of the first entry is and
‫then zero curly brackets.
‫And here I simply call the array two and I access its position zero and it's position two.
‫Why is that?
‫Because this is the first.
‫Roe pretty much a first position.
‫And then that here is the second.
‫Actually, this one here is the third position, which is two.
‫So we start off by zero or with 0012, and that's zero one and two.
‫So five is these two.
‫And now let's see what the console has to tell us if we run this code.
‫So let me run that.
‫And that we are the values in the middle of the first entry is five.
‫So it's a little similar to a two RD array, but it's not not really an 2D array, so it's an array
‫within an array.
‫All right.
‫So now a little challenge for you.
‫Try to get all the values within the array onto the console.
‫All right.
‫I hope you tried it.
‫In order to do that, I'm going to use a for loop four and I equals zero I smaller than eject array
‫two dot length I plus plus and it doesn't matter whether you choose directory two or the normal directory,
‫then I need to get the element.
‫So I'm just going to say c w element at position zero is going to be my i.
‫So it just says okay, the first element which is like the first row, then I need another for loop
‫four int j equals zero j lower than jack the ray two at the position i dot length because this way what
‫I'm getting is the length of the array within our direct array.
‫All right.
‫So now let's also use the console, CW console, right line.
‫And here it's going to say value zero is our jagged array at position i j and maybe we create an empty
‫space in between.
‫So now let's run it again.
‫And it says the value in the middle of the first entry is five.
‫That's the one that we had before.
‫Now, element zero.
‫It's two, three, five, seven, 11, and element one is one, two and three.
‫So we.
‫Got all the values from our degree.
‫All right.
‫So here, once again, a little presentation of how this could look like.
‫So you have a normal array of type int that as we have seen before and it has a index.
‫And then with this index it's on zero.
‫So index zero is for example, 15, index one is 21, index to 23 and index three is 13.
‫So these are normal values.
‫So they are just integers.
‫But now with the reject array, we also have these indexes, but the array itself stores arrays.
‫So here for example, this jagged array has at its index zero, it has an array with three values,
‫then its position one or its index one, it has another array at its index, two, it has another array
‫and so forth.
‫So that's how you can imagine jagged arrays.
‫So now that you have seen how to use jagged arrays, we will go over to using arrays as parameters in
‫the next video.