‫Instructor: Hi, within this lecture,
‫we are going to learn a data structure called arrays.
‫So we're gonna see what an array is
‫and we're going to see some of the occasions,
‫that we may want to use arrays insight.
‫So over here we have our main class,
‫but I believe we have a lot of information,
‫inside of this class.
‫So in order to be more structural,
‫I'm going to change this name, okay?
‫So it's called Main right now,
‫I can call this anything I want.
‫All I have to do is just select this
‫and right click it to change its name.
‫And of course if you're doing that with an activity,
‫with another project, you can just close this down
‫and create a new project for you at this point.
‫But I'm just going to change this name,
‫by saying refactor and rename, okay?
‫Because there is no point calling that main.
‫I'm going to call this variables,
‫because that's what we have covered here, right?
‫So I'm going to say variables,
‫with a upper case V and hit enter.
‫As you can see, as I type over here,
‫it changes the file name as well.
‫So if I run this right now,
‫I will get the same output, but this is okay for variables.
‫Now I'm going to create a new Java class.
‫So with inside of this class,
‫we are gonna go and see arrays.
‫So I'm going to name this array
‫and I'm just doing this for to be structural,
‫I could have just written it down,
‫in the same variables part as well,
‫but it's better to write it in here.
‫So of course, within this class,
‫we have to have that main function again.
‫So I'm going to create it first,
‫public static void, main
‫and remember, you have to open and close parentheses,
‫inside of this main method
‫and this is the entry point for our Java code.
‫So we need to write it exactly like this,
‫with a string array, by the way, this an array,
‫we are going to see what it is right now
‫and we're going to open and close curly braces,
‫not with a C inside.
‫Here we go.
‫Now we have our entry point.
‫Now we can write whatever we want inside of this method.
‫So what is an array?
‫Array is a data structure to store multiple values.
‫For example, you can store multiple strings,
‫you can store multiple in integers
‫or multiple floats or doubles inside of an array, okay?
‫You can store any data type that we have learned,
‫in the previous lectures.
‫So that you can actually reach them,
‫within a single variable.
‫Just imagine that you have thousand strings on the internet,
‫that you want to download.
‫So rather than creating a thousand variables,
‫for different strings,
‫you can create a string array
‫and then store everything inside of that array,
‫so that you can reach it any time you want.
‫Let's do that.
‫So I'm gonna write string, like if I,
‫as if I am creating a string,
‫but at the end of the type over here,
‫I'm gonna put this notation, okay?
‫This syntax, it means that I'm creating a string array,
‫so after the type, what do we write?
‫We write the name of the object, name of the variable,
‫like my string array, okay?
‫Then we can create, we can actually assign the value.
‫But before we go into the values,
‫there are a couple of ways to initialize string arrays
‫or any type of arrays that you may think of.
‫And I'm going to show you every one of them, don't worry.
‫First of all, we are gonna go for a traditional,
‫conventional way, which is initializing it,
‫by saying new string array like this, okay?
‫But as you can see, there is an error over here,
‫because it expects some parameter inside of this braces.
‫So for example, I'm gonna say three.
‫So what does this three represent?
‫It represents that there's going to be three elements,
‫inside of this my string array,
‫it will contain three strings.
‫So all we just did right now is to create a my string array
‫and said that it's going to be three elements,
‫inside of that string array.
‫If I say my string array right now, I can open this braces
‫and I can just start,
‫giving out the values over here like this.
‫So the first element inside of a string,
‫has the index of zero.
‫So whenever we say my array zero,
‫it means that first element in that array.
‫So in this case, I'm saying that James,
‫is the first element inside of my string array
‫and I can make that as long as I reach the capacity,
‫that I have given in the initialization part.
‫For example, I can go for Lars
‫and I can do this one more time, okay?
‫And I can just say something like, Kirk over here.
‫Now my string array has three elements
‫and let me try to write them down.
‫So if you do this, if you try to run this, okay?
‫Right click on main and say Run.
‫You won't see James, Lars, and Kirk like this.
‫You will see, yep,
‫this is a string array at this memory part, okay?
‫So in order to see the individual elements,
‫inside of an array, you have to specify,
‫the index of that particular element,
‫that you are trying to reach.
‫For example, like this,
‫if I say my string array zero, then I can see James.
‫If I wanna see Lars, then I can go for one.
‫And as you can see, it works.
‫Now I have a variable
‫and I have different values inside of that variable,
‫I can reach them by specifying their indexes.
‫So this works.
‫And again, if I had a 1000 array elements over here,
‫then it would work fine as well,
‫but maybe you can't think something like that.
‫Yeah, I had three elements over here
‫and I knew there's going,
‫there was going to be three elements, but what if I don't?
‫What if I don't know what kind of an element,
‫there is going to be over here?
‫Like how many elements?
‫I don't know.
‫I'm going to download it from the internet.
‫Maybe it'll be 900, maybe it'll be 1000.
‫How do I initialize this?
‫Don't worry.
‫We are gonna work on that later on.
‫Right now you just try to understand,
‫arrays are used in order to store values,
‫multiple values inside of one single object.
‫We can do that with integers as well.
‫For example, in my integer array,
‫will be new int array like this, okay?
‫You can just call it three for example, again
‫and you can add as many numbers as you want over here,
‫up until three elements like this,
‫my integer array zero 50,
‫my integer array one is something else like 60,
‫let's make 50, 60, and 70 with their related ages,
‫like James' age is 50, Lars' age is 60,
‫and Kirk's age is 70, okay?
‫So for example, if I reach out to second element,
‫index one in the first two list,
‫then I can get Lars and the age of the Lars with 60,
‫for example, this is a good example of leveraging arrays.
‫For example, if I had two different sources,
‫from internet to download, that I could have downloaded them
‫and just edit them in order,
‫so that they will coincide with the indexes,
‫so I can get the first index, second index,
‫in order to find the name
‫and the age of given elements, okay?
‫For example I could have done this as well,
‫int my number array, okay?
‫Rather than saying three,
‫rather than giving out the capacity,
‫I can just open curly braces over here
‫and write the numbers like this.
‫1, 2, 3, 4, 5, 7, 8.
‫Now I have the same thing.
‫I can reach the same thing over here.
‫For example, if I come over here and say two,
‫what will I get over in the logs?
‫So if you had said three, then you are completely right.
‫Why do we get three?
‫Because it's zero, okay, here and one over here
‫and two over here.
‫Number three has the index of two,
‫because the index starts with zero in the arrays.
‫So this is one of the methods that you can define an array,
‫without giving the capacity first.
‫For example, over here, if I do this,
‫my integer array three 80, okay, it would work fine.
‫But if I run this, then I will get an error,
‫because as you can see,
‫I get this array index out of bounds exception,
‫it's actually out of bonds,
‫because I have a restriction over here,
‫I specified the capacity as three.
‫Now, I should put three elements inside of this array.
‫If I put the fourth one.
‫As you can see,
‫it gives me array index out of bounds exception.
‫So if we change this to be four, I can just come over here
‫and add the fourth element as well like this
‫and I won't get any errors after all.
‫So this was an introduction to arrays.
‫We will continue learning about new kind of arrays,
‫in the next lecture.