It's time to learn how to a bitter race.
So far, we use the for loop to run through the length of an array and now you know how to create arrays
as well as how to access their values.
The last step is to learn how to update them.
So inside your Section six folder, create a new file named updating, Erase Java and inside the class,
make sure there's a main method.
Just like we can access array values from their index, we can update them all the same.
So you have an array of three elements, if you want to change an element, just to refer to its index
and then change the value.
It's that simple.
And so I prepared a fine example for us to learn about how to update race, you decided to pursue your
lifelong dream, opened up a cafe, the Java Cafe, your menu starts off pretty simple.
It offers three beverages, the espresso, iced coffee and macchiato.
So inside the class, we're going to store the menu in one array string menu.
Is going to equal an array of three string values.
Espresso.
Iced coffee and macchiato.
Now, I don't know about you, but I'm a bit tired of using loops to print theory.
Java has a function called to string.
It takes your array as an argument and it returns a string that we can print.
So first to write the word erase, wait a bit and click on the first option and what that's going to
do is auto import the utility class.
And so from the erased utility class, we can use the two string method.
The two string method is going to take your array as an argument and return a string copy, we can store
the string copy in a string variable.
And this is really convenient because we can now print the contents of menu as one long string.
Or on the code and nice to string returns a string copy of the array, which you can easily print.
Now, instead of storing the return value, then printing it, I'd rather just print the return value
in one line.
And your coffee is all set up and people are going nuts, but nobody's buying the macchiato, so you
decide to sell lattes instead.
So we're going to do is we're going to update the value at index two and set it equal to latte.
All right, Perfect's.
Now, let's say you want to add more items to the menu, is it possible to change the array length?
And the answer is a big fat no, you cannot change the length of an array.
And that's really unfortunate because your Java cafe is becoming really popular and you need to expand
your menu to accommodate new customers.
There is a lot of demand for house blend and dark roast, but the problem is once you create an array,
you cannot resize it.
The current array only has a room for three items.
So what I can do is create a new array that can store five string values.
So inside the code, I'm going to create a new array that can hold five menu items, string menu.
Is equal to new string that can hold five elements.
The new area has five elements with indexes of zero to four, and by default, the values are null.
Now we're going to create a loop that runs through every item in the old menu, so 4:00 a.m. is equal
to zero.
Remember, indexing an array always starts at zero.
Just at the last index in the array is always one less than the array's length.
And during each run, we're going to print the index I.
As well as the value that we're indexing in the menu.
All right, we're off to a good start.
It runs through the length of the older ray and it prints every element.
Now we need to copy each element from the old array into the new array.
So I'm going to delete this code.
And I'm going to set every element a new menu.
Equal to the element in menu with the same index.
And so from a visual standpoint, this loop runs three times in copies, every value from menu to the
same index as new menu.
To prove this, I'm going to print the contents of new menu as a string.
And as you can see, the first three volumes are copies from the old array, because there was only
three values, the copy from the old array.
But we still have to add house blend and our coast our menu.
So what we'll do is we'll update the element in the next three set that equal to house blend.
And we'll update the elemental index for and set that equal to dark roast.
Rewriting, running the code.
And the Java Cafe is back in business, we successfully updated the menu and we're ready to accommodate
more customers.
From a visual standpoint, you updated the elements and indexes three and four.
Now, your menu is complete and your customers are happy.
In this lesson, you learn how to a bitter race.
You opened up your first cafe, the menu only had three items, your cafe became very popular, so you
decided to add house blend and darkness to your menu.
The problem was you cannot resize an array once you created.
You copied the three elements from menu and to new menu.
And then you manually updated indexes three and four with houseplant and Dark Roast.