We can use a phrase, the store many values at the same time.
I hope you're proud of yourself, because so far you covered most of the fundamentals you need to build
Java applications, namely you learn how to store and update information using variables, control how
your code runs using effluents, which organize your code into functions and running your code many
times using far loops and while loops, the last thing you need to learn about is arrays.
So in this lesson, you're going to use arrays to store many values at the same time.
First thing you need to do is create a new job, a project named Section six, and inside the project
create a new file named intro to Erase Java and as always, make sure the class has a main method.
We can store values into an array, an array lets you store many values at a time.
Arrays in Java look like this, if I want to store many integers at once, then I can put square brackets
next to the integer type and now I can store many integer values in one variable.
That being said, an array can hold many values, but all of them have to share the same type.
For example, integers, stars, an array of integer values, words that stores an array of string values.
Decimals stores an array of double values and letters, stores, an array of characters.
And so from a visual standpoint, this is how a variable stars an array.
Looking at all these arrays, you might be thinking, what is the dot, where did that come from?
Good question.
I'm glad you asked.
The DOT is a reference.
A variable cannot store the array directly at stores, a unique reference, and it's the reference that
points to the array.
This is a really important distinction to make.
I'm going to include these bullet points in your cheat sheet, because I think this is something that's
really important to be aware of, especially as we go through the remaining code.
So instead of your class, you're going to find an array that stores, kingdoms, I'm going to put an
array that can hold many strings.
The variable is going to be called kingdoms and it's going to store for string values.
Northumbria and East Anglia.
Yay for short.
Now, try printing the array.
And what is this seven to six debate, what does this spring represent?
It's kind of weird.
Well, not really.
Think about it.
Pretty line Prince.
Whatever is inside the verbal kingdoms and what's inside the verbal is a unique reference represented
by a white dot.
So instead of actually printing the array value itself, what we're printing is the reference that points
to it.
So how do we access values from an array, how do we access an array elements, you can access an element
by referring to its index number.
By the way, an element is just a value inside of an array in the coding world.
We say elements.
And every element has an index, an index is a number that represents a position.
It starts at zero and counts up the number of elements.
So Jane would have an index of zero.
Joe would have an index of one.
And Joe would have an index of two.
If you want to access an element, just type Geary's name, followed by square brackets and inside the
brackets, write the index of the element that you want to access.
So index is zero would point to the first element in the array.
Jane, index one Joe and index to Joe.
And so back in our code, we can print each Kingdome individually, I'm going to print the kingdom at
Index zero Mersea.
The kingdom at index won.
The kingdom at index two and the one in the next three, I can now run the code.
And as you can see it, access to each element by referring to its index number and then it printed
it.
I prepared this visual for you just so that you can see what's going on.
OK, now, more often than not, you're going to specify an index that's out of range and Jeb is going
to yell at you and crush, you cannot refer to an index number that doesn't exist.
You cannot refer to an index beyond the bounds of the array if you try to.
Jeb is going to throw an error and it's going to crash.
And so the kingdom, right, it had four elements, but because we start counting at zero, the last
element is going to have an index that's exactly one less the length of the array three.
So in your code, if we try to access an element that index for let's do just that.
Run the code.
And Java crashes, the last print statement throws an error because the next four is out of bounds.
You cannot access an element from an index that doesn't exist.
So I'm going to delete the last print statement and clear the output.
In this video, you use the arrays to store many values in the same variable and then you accept each
element using its index, you define a string array that stores kingdoms, each element has an index
and you access each kingdom by referring to the index number.
From a code perspective, the runtime appears as follows.
I also showed you that we can't refer to an index number that doesn't exist, the kingdom's variable
had four elements, and because we start counting from zero, the index ranges from zero to three.
So if you try to access an element, that index for Jeb is going to throw an error and your code is
going to crash.