Transcript from the "Arrays" Lesson
>> Okay, in this section we're going to talk about arrays. So if we think about primitive variables, they can all hold a single value. Arrays can hold multiple values, but they must all be of the same type. So here's an example of a primitive variable. We have int age = 21;, it holds one value, right?
[00:00:23] An array would be something that holds multiple values. So if you think of something like a lottery ticket, a lottery ticket has multiple numbers associated with it, right? And therefore, this variable would need to be able to hold all of those values. So let's look at this definition and I'll explain each part of this.
[00:00:48] So it starts with the data type of the array. What is the data type that this array can hold? Again, all of the values within this have to be of the same data type. So an array can hold like any sort of data. But within a single array, all of the data must be of the same type, all right?
[00:01:10] The next part of that is a set of square brackets. And those brackets indicate that this is an array. So we've seen this with the string args and main, those square brackets there. Basically saying that main accepts an array of strings, right? So this is the same thing.
[00:01:29] Those brackets can appear after the data type or it can appear after the name of the variable itself. It doesn't matter, all right? LottoTicket is the name of this array. So after you have the data type in the brackets, then you define the name of the array. And then this equal sign is we are going to essentially specify that this will be a new array.
[00:01:58] And inside of the bracket with the data type, a number. And that number represents how many elements this array can hold. If we were to look inside of an array, this is what that would look like. So this int LottoTicket, notice this time I put the brackets after the name to let you see that you can put it either after the data type or after the name, either one, it really does not matter.
[00:02:26] And then, in the memory location it would look something like this. So we would have like six different slots for each of the values. And then, each of those slots has an address. Which is an index, and so the very first slot, the index would be 0. These indices start at 0, 0 1, 2, 3, 4, and 5, all right?
[00:02:54] So another way that you can declare an array is if you know the values upfront. Sometimes you don't use things like a loop or something to get the values. But if you already know the values upfront, you can go ahead and initialize that array with the values. So notice here inside of a set of curly braces after the equal sign.
[00:03:16] I no longer have new, I no longer have the data type, I just go ahead and do a comma, delimited list with the values of the array, and then you see how they are stored. So each one of those values gets its own little memory location, okay? So if I wanted to get a Lotto ticket with the index of 2, what would be the value?
>> Not quite.
>> 22. So, index of 2 would be like literally 22, but it would be the third element of the ray since it starts at 0. The length of the array, so we said that this was 6, or we didn't even say how many elements this one was.
[00:04:07] In this declaration, we just added the values. You cannot change the length once it is defined. So I would not be able to later say, hey, let's add a seventh element to Lotto ticket.