Check out a free preview of the full Java Fundamentals course

The "Arrays" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses how to define an array variable. Arrays can hold multiple value, but only if they are the same data type.


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?

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.

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?

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.

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.

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.

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?

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.

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?

>> 14.
>> Not quite.
>> 22.
>> 22.
>> 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.

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.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now