WEBVTT 1 00:00:02.950 --> 00:00:10.510 John Sader: Everyone, and welcome to my video on traversing 2D arrays in Java. Let's get started. 2 00:00:11.970 --> 00:00:32.090 John Sader: So let's look at one dimensional arrays first so you can see I use Java code to declare an instantiate a on d array. And then I sort of made this visualization of what that really looks like. Right? So arrays are 0 based, meaning that their index starts at 0. 3 00:00:32.619 --> 00:00:46.840 John Sader: So if we look at that the number one box. That's our 0 index. And then our first index is where the 2 is, and our second index is where the 3 is. So how do we traverse a one d array? Well, we're going to use a for loop. 4 00:00:47.040 --> 00:01:10.280 John Sader: so our for loop. is gonna use I and it's going to increment over the array. meaning that as I, meaning that I points to our index, right? So we're gonna start with I at 0. And then we're gonna have the condition. I is less than array dot length, which is a a property of our integer array 5 00:01:10.940 --> 00:01:19.890 John Sader: meaning the link. So that's gonna be 3, right? Because we have 3 elements in this array. and then the body, we're just doing a simple print statement. Okay. 6 00:01:20.050 --> 00:01:24.280 John Sader: so our first iteration. I is going to start at 0. 7 00:01:24.290 --> 00:01:40.280 John Sader: So when we call a ray at the I index, we're calling array at the 0 index, which is one. Okay? So then, we're incrementing. I so you can see when I equals 0 pointing at one, we increment I for the second iteration through the loop. 8 00:01:40.310 --> 00:01:43.180 John Sader: I now equals one, which means it's pointing at 2. 9 00:01:43.520 --> 00:02:01.299 John Sader: Then we go one more and I equals 2. It's pointing at 3 At this point I increments, and I equals 3. that's also our array dot link. So is 3 less than 3. No, it's not so. We're done with the loop. We jump out and move on to the rest of our code. 10 00:02:02.670 --> 00:02:17.500 John Sader: Okay. So now let's look at a 2D array. so a two-dimensional array basically just means. it's an array of arrays. Okay? So whereas with our one dimensional array, every element was an integer. 11 00:02:17.530 --> 00:02:24.760 John Sader: Now, every element is an array. Okay? And this specific example, each of those 12 00:02:24.970 --> 00:02:26.700 John Sader: array elements 13 00:02:27.140 --> 00:02:36.249 John Sader: have elements, right? Because they're an array. And those elements are integers. Okay? So we see, we have 3 elements in our outer array. 14 00:02:37.160 --> 00:02:44.600 John Sader: And then within each inner right, we also have 3 elements. So we have 9 total elements. okay. 15 00:02:46.030 --> 00:02:47.200 John Sader: so 16 00:02:47.870 --> 00:02:52.190 John Sader: let's break this down going with using a for loop. Okay? 17 00:02:53.010 --> 00:03:05.849 John Sader: So you'll notice our outer for loop where I equals 0. I is less than a rate out length. I plus that is exactly the same as the one dimensional right? Right? Because 18 00:03:06.010 --> 00:03:09.200 John Sader: we're doing the same thing. We are 19 00:03:09.530 --> 00:03:12.249 John Sader: going through each element in the array. 20 00:03:12.870 --> 00:03:23.219 John Sader: What's different is the body because we have a nested for loop. And the reason we have that is because at each element we need to further 21 00:03:23.250 --> 00:03:27.449 John Sader: look into it and look at the integers in each element. Right? 22 00:03:27.730 --> 00:03:30.600 So in our first array we have 1, 2, 3, 23 00:03:31.020 --> 00:03:42.000 John Sader: so J is going to be used as the like sub index. of the specific element. So J equals 0 24 00:03:43.060 --> 00:03:53.160 John Sader: and I equals 0. So we're pointing at that one. Okay? And I is gonna stay 0 for the whole time. We're looking at that first element because we're going to execute 25 00:03:53.280 --> 00:03:59.930 John Sader: the entirety of that second, the nested for loop. before we increment. I 26 00:04:00.550 --> 00:04:16.169 John Sader: okay, I hope that makes sense. So Jay, Jay starts at 0 right? And this is going to be just like the last example, we're going to increment. J, it equals one. Now, we're pointing at that 2. And then we're going to increment again to 2. Now we're pointing at that 3. Okay. 27 00:04:17.209 --> 00:04:20.070 John Sader: now, at this point J will equal 3, 28 00:04:20.350 --> 00:04:28.900 John Sader: and it won't meet that for loop condition. So we're gonna jump out of that for loop. And we're gonna get to the bottom of our outer for loop 29 00:04:28.970 --> 00:04:36.979 John Sader: where I gets incremented. Okay, so I is going to increment. And then we go back into our 30 00:04:37.300 --> 00:04:41.729 John Sader: nested for loop. Right? And J is, gonna we're gonna start from 0 again. 31 00:04:41.790 --> 00:04:45.750 John Sader: So now I equals one and J equals 0. We're pointing at that 4. 32 00:04:45.830 --> 00:04:52.810 John Sader: And then what's gonna happen? We're gonna increment J and point at that 5. And then we're gonna increment J again and plant that 6. Okay. 33 00:04:54.010 --> 00:05:12.239 John Sader: get to the end of that. And now we're gonna increment I and J is equal to 0. And then we keep going until we have traversed every single element in this 2D array. At this point. I increments to 3, and we jump out of our for loop. 34 00:05:12.440 --> 00:05:18.090 John Sader: and we execute the rest of our code. all right, that's it. Thank you.