Hello there, and welcome back to this job development course. So, last time we went over for loops, and you had some homework to do, your job was to write a program that would get a variable from the console, and then print the factorial of that variable. So here is essentially what you should have done. So first of all, I create my scanner to get the value from the console. Then I do int a equals scanner dot next integer, then I create a for loop, which creates an integer i is equal to a minus one. So if we are for is going to be equal to three, then I tell the loop to keep on executing while I is more than zero.
Then I said every time we loop, we're going to subtract one from my, then I just assign A to A times I was going to be first A times three, then a times two and then a times one which is what happened. tutorial is all about. Now, I can actually just do times equals that actually just makes the code a little bit less. So if you remember, this is an assignment operator that we went over, it does exactly the same thing. Alright, and then I just print a after loop is executed. So we can test this out, we can have five factorial, that's gonna be 124 factorial, it's gonna be 24.
And they're gonna have 10 factor, that's going to be 3,628,800. So there we go. It's pretty simple application. Nevertheless, it does take some mathematical thinking and logical thinking as well. But it actually is a very good program to understand how exactly you could work with data and how you could write programs in general. So yeah, anyway, without further ado, let's get into this lesson.
All right. So today we're going to be going over arrays. So let's create our array class. New class, let's call it arrays. And main as always, and finish. Alright, there we go.
Delete the comment as always as well. All right, so what is an array? An array is essentially just list. So how do we create an array? Well, we need a data type, as always, so let's say that we have an array of integers. So int, and then we open and close, square brackets, open close.
And then we need to give our array a name. So let's just call our array A and then we're going to assign it some value. So in this case, let's say in our array, new integer array, and now we need to input some number into these square brackets. So this number will be v length of our array. So let's use input 10. And there we go.
As you can see, there are no more errors. Now, let's assign a value to an array. So we can do a open brackets, zero. And remember, arrays, start counting from zero. So the first element in an array will be zero, not one, a zero is equal to two. Okay, there we go.
And now we can do system dot out, dot print ln AE zero. So this is how we get a value from an array. We can assign a value to an array by doing a zero equals two, or we can get a value from an array by just doing a zero. So let's go ahead and run this. And as you go, and there you go, you can see it prints to fix that. We'll get that since that's what we assigned to it.
All right. So, that is generally how we work with the race. Now, there are a couple things to understand. So first of all arrays can be of any type. So we have an integer we can have it be as long as well. It can be a short by string, boy Boolean, character anything.
An array can be of any type, since it can be of complex type like strings. We can also have it be of any complex type so like for example scanner All right, there we go. So, as you can see, we're just creating a new scanner array with 10 values, then assigning the first value to new scanner. Whoops new scanner putting system to As the place where whoever can get it from, and then printing the scanner that we put into a zero dot next int. So now if we run this, we get three. There we go.
All right. Now let's take a look at some possible ways we can manipulate arrays. So we can have an integer array A, it's gonna be equal to new int array with 10 values. So first of all, we're going to create a method which will fill the array with values from one to nine. There we go. All right, there we go.
So there's a method for filling the array with the values from one to nine. And now we're going to have a separate method for printing the array. Or I'm saying no, it's not. It's not a method. This is a loop. No, a method is a completely different thing.
This is a loop. So we're gonna have an array for a loop. for filling the array, and then a loop for printing the array. All right, there we go. All right, there we go. So now if we run this code, we get the values from zero to nine.
Since it starts from zero then just inserts into this, for example, the zero value of the array is zero, the first value and enters one, second two, and so on. So we could also if really want to do we can do math dot random, times 100, and then cast that to integer. All right, there we go. And so now we'll just get a random around values as you can see, alright. So that is how we use method to use loops to work with arrays. And you can see now how the for loop is starting to end, you're starting to understand how the for loop sort of interconnects with arrays and why we need the for loop for arrays, all right.
So, another message that we can, which we can fill arrays is actually without doing new end, we just do two curly brackets and then just the numbers that we actually want to insert. There we go. And now if we print the array, we get the numbers that we inserted, there we go. So when you do this, when you do it like this, it actually will just put set the length of the array as as many numbers as you introduced in this case, their length will be 1-234-567-8910 years the length will be 10. All right. Okay, so that is essentially how we work with arrays.
Now we can also have an array of arrays. So this works a little bit differently. So we can have an integer, and then an array of arrays. And then this we would assign as an array. And then inside we're going to put two arrays, each of which will have three values. There we go.
So as you can see here, this is just an array of arrays. So there really isn't, isn't anything difficult here. This is just a multi dimensional array. So we can get a value from the array by doing system dot out, dot print ln a zero, which will return this array here. And then from that array, we're going to get one which will return the second value or two. So now this will print to Alex If we do one one, this will get this array here.
And this value here, let's change this to 557. Just to prove the point here. So yeah, so it's gonna get this array and then this value, second value. All right, let's take a look. And there we go. 567 as expected.
So we can also use for loops in this array, although with here we're going to have to use a multi dimensional for loop as well. So we just do for int i equals zero, i is less than a dot length. I plus plus. And then for int i one is equal to zero, i is less than a, I dot length. And then I one plus plus, no, sorry, it's gonna be a one is listening. I think there we go.
And now System dot out dot print ln. Ay ay ay one. Alright, there we go. So now if we run this, we get 1233561. So this is, this is sort of the actual race that we, that we created here. So that's how we print a multi dimensional array.
So what do we do here for? Well, first of all, we declare a for loop with an i variable, which is equal to zero, then while i is less than a dot length, so it's going to be two. In this case, there's since there are two arrays, we're going to we're going to execute the loop, then we have another loop inside, that's gonna have an i one variable, which I want is going to be the less than a dot length. So for example, if I zero is going to get this array, and then this length was going to be three and then we just add one to it one every single time we loop and now it prints eight, I was going to get the array under I so for example, is saying at 050 and this one is as if I as one and then I want so just the value inside that array and result we get this printed array.
All right. And so that is it. How we work with a race. All right, so one last thing we actually should go over with arrays is that they don't really work when we print them. So if you want to print an array, we don't, we would have to iterate over the array and then print every single value. And iterating essentially is going to every single value and then printing it manually.
We can't do System dot out dot print ln limited, we can, it's just wrong A. So this will actually just give us this, this is just an N, the ID sort of of the array. So if you want to print the array, we would have to go over every single value of the array and then print that as well. Okay, and one final thing that we're just going to go over just this isn't actually really doesn't have much to do with arrays, if we wanted to print a multi dimensional array, but have each sort of value be on separate lines. So each array be on separate lines that first prints 123 and then on the next line prints, three 561 one We would have to do a system out System dot out dot print here and then plus, comma. And then here, we would have to do System dot out dot print ln.
There we go. Just an empty print line. And now if you run this, as you can see it printed correctly on separating lines 123335671 because System dot out dot print will print and then not break the line. So it just, it'll just print on the same line. And then when if if we print again, it'll print on that same line, but then print ln goes to the next line. Alright, so Okay, so now in this lesson, we've actually done quite a bit.
So we've learned the very basics of arrays. We've learned how we can use for loops with arrays, and we've also gone over multi dimensional arrays. Now, multi dimensional arrays aren't necessarily just two dimensional arrays. Multiple arrays can be infinitely dimensional. So I think Well, I mean, obviously, it's not infinitely many dimensional because every computer has its limitations, but just being we can have, you know, four dimensional arrays. So we can in here we can have an array.
And another array, I think array array. Right, there we go. So yeah, there can be infinitely infinite amount of arrays, you know, an endless amount of arrays. So yeah, it doesn't necessarily have to be just two dimensional. But most of the time, we're going to be using one dimensional or two dimensional arrays, either way. All right.
So anyway, let's find some homework. All right. So for homework, your job is to take an array of values which say that we have an array A and then just assign it some values, you know, just some value to really matter all that much. And your job is to print the average of those values. So for whatever our You get your job is to just print the average and it should be dynamic. So if I add more values, it should be able to automatically update the average without, you know, changing any of the code.
So anyway, it's pretty simple homework. You will be using for loops for this. So just keep that in mind. And yeah, without further ado, then I wish you luck and I'll see you next time soon.