Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,330 --> 00:00:02,370
It's time to learn how to a bitter race.
2
00:00:03,710 --> 00:00:08,810
So far, we use the for loop to run through the length of an array and now you know how to create arrays
3
00:00:08,810 --> 00:00:10,880
as well as how to access their values.
4
00:00:15,960 --> 00:00:17,970
The last step is to learn how to update them.
5
00:00:21,400 --> 00:00:27,090
So inside your Section six folder, create a new file named updating, Erase Java and inside the class,
6
00:00:27,100 --> 00:00:28,570
make sure there's a main method.
7
00:00:34,420 --> 00:00:38,710
Just like we can access array values from their index, we can update them all the same.
8
00:00:41,300 --> 00:00:46,010
So you have an array of three elements, if you want to change an element, just to refer to its index
9
00:00:46,010 --> 00:00:47,300
and then change the value.
10
00:00:47,330 --> 00:00:48,320
It's that simple.
11
00:00:50,690 --> 00:00:56,090
And so I prepared a fine example for us to learn about how to update race, you decided to pursue your
12
00:00:56,090 --> 00:01:01,910
lifelong dream, opened up a cafe, the Java Cafe, your menu starts off pretty simple.
13
00:01:01,910 --> 00:01:06,080
It offers three beverages, the espresso, iced coffee and macchiato.
14
00:01:08,820 --> 00:01:13,350
So inside the class, we're going to store the menu in one array string menu.
15
00:01:16,750 --> 00:01:19,450
Is going to equal an array of three string values.
16
00:01:20,680 --> 00:01:21,490
Espresso.
17
00:01:24,300 --> 00:01:26,100
Iced coffee and macchiato.
18
00:01:33,690 --> 00:01:37,360
Now, I don't know about you, but I'm a bit tired of using loops to print theory.
19
00:01:37,800 --> 00:01:39,630
Java has a function called to string.
20
00:01:39,810 --> 00:01:43,380
It takes your array as an argument and it returns a string that we can print.
21
00:01:48,340 --> 00:01:54,190
So first to write the word erase, wait a bit and click on the first option and what that's going to
22
00:01:54,190 --> 00:01:56,440
do is auto import the utility class.
23
00:01:57,530 --> 00:02:01,220
And so from the erased utility class, we can use the two string method.
24
00:02:03,980 --> 00:02:09,259
The two string method is going to take your array as an argument and return a string copy, we can store
25
00:02:09,259 --> 00:02:10,940
the string copy in a string variable.
26
00:02:20,970 --> 00:02:25,800
And this is really convenient because we can now print the contents of menu as one long string.
27
00:02:41,110 --> 00:02:46,840
Or on the code and nice to string returns a string copy of the array, which you can easily print.
28
00:02:50,550 --> 00:02:54,960
Now, instead of storing the return value, then printing it, I'd rather just print the return value
29
00:02:54,960 --> 00:02:55,680
in one line.
30
00:03:04,650 --> 00:03:10,770
And your coffee is all set up and people are going nuts, but nobody's buying the macchiato, so you
31
00:03:10,770 --> 00:03:12,300
decide to sell lattes instead.
32
00:03:12,930 --> 00:03:17,010
So we're going to do is we're going to update the value at index two and set it equal to latte.
33
00:03:29,680 --> 00:03:30,880
All right, Perfect's.
34
00:03:34,660 --> 00:03:39,310
Now, let's say you want to add more items to the menu, is it possible to change the array length?
35
00:03:39,800 --> 00:03:44,740
And the answer is a big fat no, you cannot change the length of an array.
36
00:03:45,580 --> 00:03:51,070
And that's really unfortunate because your Java cafe is becoming really popular and you need to expand
37
00:03:51,070 --> 00:03:53,180
your menu to accommodate new customers.
38
00:03:53,350 --> 00:03:59,320
There is a lot of demand for house blend and dark roast, but the problem is once you create an array,
39
00:03:59,320 --> 00:04:00,670
you cannot resize it.
40
00:04:01,090 --> 00:04:04,280
The current array only has a room for three items.
41
00:04:04,900 --> 00:04:09,550
So what I can do is create a new array that can store five string values.
42
00:04:15,220 --> 00:04:20,050
So inside the code, I'm going to create a new array that can hold five menu items, string menu.
43
00:04:24,350 --> 00:04:28,220
Is equal to new string that can hold five elements.
44
00:04:30,580 --> 00:04:35,710
The new area has five elements with indexes of zero to four, and by default, the values are null.
45
00:04:40,230 --> 00:04:45,300
Now we're going to create a loop that runs through every item in the old menu, so 4:00 a.m. is equal
46
00:04:45,300 --> 00:04:45,920
to zero.
47
00:04:46,410 --> 00:04:48,740
Remember, indexing an array always starts at zero.
48
00:04:48,760 --> 00:04:52,920
Just at the last index in the array is always one less than the array's length.
49
00:05:04,590 --> 00:05:07,380
And during each run, we're going to print the index I.
50
00:05:14,290 --> 00:05:16,780
As well as the value that we're indexing in the menu.
51
00:05:26,540 --> 00:05:28,190
All right, we're off to a good start.
52
00:05:28,970 --> 00:05:32,510
It runs through the length of the older ray and it prints every element.
53
00:05:34,040 --> 00:05:38,480
Now we need to copy each element from the old array into the new array.
54
00:05:42,080 --> 00:05:43,700
So I'm going to delete this code.
55
00:05:45,080 --> 00:05:47,570
And I'm going to set every element a new menu.
56
00:05:50,630 --> 00:05:53,660
Equal to the element in menu with the same index.
57
00:06:00,770 --> 00:06:07,130
And so from a visual standpoint, this loop runs three times in copies, every value from menu to the
58
00:06:07,130 --> 00:06:08,660
same index as new menu.
59
00:06:22,410 --> 00:06:25,920
To prove this, I'm going to print the contents of new menu as a string.
60
00:06:42,040 --> 00:06:46,450
And as you can see, the first three volumes are copies from the old array, because there was only
61
00:06:46,450 --> 00:06:48,400
three values, the copy from the old array.
62
00:06:49,460 --> 00:06:52,520
But we still have to add house blend and our coast our menu.
63
00:06:55,070 --> 00:06:59,540
So what we'll do is we'll update the element in the next three set that equal to house blend.
64
00:07:09,630 --> 00:07:13,800
And we'll update the elemental index for and set that equal to dark roast.
65
00:07:19,690 --> 00:07:20,740
Rewriting, running the code.
66
00:07:24,600 --> 00:07:30,660
And the Java Cafe is back in business, we successfully updated the menu and we're ready to accommodate
67
00:07:30,660 --> 00:07:31,740
more customers.
68
00:07:33,340 --> 00:07:36,880
From a visual standpoint, you updated the elements and indexes three and four.
69
00:07:40,970 --> 00:07:43,640
Now, your menu is complete and your customers are happy.
70
00:07:47,240 --> 00:07:49,310
In this lesson, you learn how to a bitter race.
71
00:07:50,480 --> 00:07:56,390
You opened up your first cafe, the menu only had three items, your cafe became very popular, so you
72
00:07:56,390 --> 00:07:58,850
decided to add house blend and darkness to your menu.
73
00:07:59,360 --> 00:08:02,210
The problem was you cannot resize an array once you created.
74
00:08:04,460 --> 00:08:07,610
You copied the three elements from menu and to new menu.
75
00:08:15,670 --> 00:08:20,380
And then you manually updated indexes three and four with houseplant and Dark Roast.
7692
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.