1 00:00:00,700 --> 00:00:04,390 Before we start, did you try solving the workbook yourself? 2 00:00:04,420 --> 00:00:10,180 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:10,870 --> 00:00:17,440 Welcome to Workbook 6.12 and this one you decide to play the lottery and you're feeling good about these 4 00:00:17,440 --> 00:00:18,260 numbers. 5 00:00:18,280 --> 00:00:24,460 I guess you had a dream about them or something, and now it's telling us use a raised copy of to copy 6 00:00:24,460 --> 00:00:29,830 all the elements of ticket into ticket two and then change the third value to 54. 7 00:00:29,920 --> 00:00:33,280 Now what I'm going to do actually is the exact opposite. 8 00:00:33,310 --> 00:00:41,650 Instead of copying every value from ticket into ticket two, what I will just do is set ticket to equal 9 00:00:41,650 --> 00:00:42,400 to ticket. 10 00:00:44,390 --> 00:00:49,940 And if I were to change the value at index 2 to 54, what do you think would happen? 11 00:00:51,340 --> 00:00:54,430 Let's visualize the runtime and find out. 12 00:01:05,450 --> 00:01:07,820 So the variable ticket is zooming in. 13 00:01:07,850 --> 00:01:12,050 You'll see that it stores a reference that points to an array. 14 00:01:13,110 --> 00:01:18,540 If you set ticket to equal to ticket, all you're doing is copying over the reference. 15 00:01:20,930 --> 00:01:26,740 So now take it two shares the same reference at ten, pointing to the exact same array. 16 00:01:26,750 --> 00:01:32,510 So whatever changes I make through the ticket to variable will also affect ticket. 17 00:01:35,070 --> 00:01:36,210 Isn't that crazy? 18 00:01:36,240 --> 00:01:41,130 So this is the reference trap by setting ticket to equal to ticket. 19 00:01:41,160 --> 00:01:46,470 All we're doing is copying over the reference that points to the exact same array. 20 00:01:46,500 --> 00:01:47,820 That's no good. 21 00:01:47,880 --> 00:01:54,390 What we need to do is instead of copying over the reference copy over all the values, which means we 22 00:01:54,390 --> 00:01:57,420 can say erase copy of. 23 00:01:57,570 --> 00:02:03,450 We're going to create a copy out of the original array and we're going to copy the full length of the 24 00:02:03,450 --> 00:02:03,990 array. 25 00:02:05,760 --> 00:02:07,680 All right, So if we try this again. 26 00:02:13,900 --> 00:02:17,170 Ticket store is a reference that points to this array. 27 00:02:18,180 --> 00:02:19,230 Ticket to. 28 00:02:20,050 --> 00:02:24,440 Store is another reference that points to a copy of the original array. 29 00:02:24,460 --> 00:02:30,200 So now if I change, the state of ticket to ticket remains unaffected. 30 00:02:30,220 --> 00:02:35,830 And it's very nice that breakpoints allow us to visualize the fact that the variable store is a reference 31 00:02:35,830 --> 00:02:42,550 that points to an array at ten points to one array at 12 points to another array. 32 00:02:45,530 --> 00:02:49,220 Task number two, create a function that prints the ticket numbers. 33 00:02:49,340 --> 00:02:53,980 All right, so the function name is print ticket numbers. 34 00:02:53,990 --> 00:03:02,690 It's going to be void, but it will take an one parameter of type int So public, let me zoom out a 35 00:03:02,690 --> 00:03:03,180 little. 36 00:03:03,200 --> 00:03:06,770 Public static void print ticket numbers. 37 00:03:08,190 --> 00:03:09,810 Event tickets. 38 00:03:12,980 --> 00:03:16,640 First thing we do is use a loop to print the ticket numbers in one line. 39 00:03:16,640 --> 00:03:21,560 So we create a loop that runs through the length of the ticket array. 40 00:03:22,370 --> 00:03:25,100 I is smaller than ticket length. 41 00:03:26,140 --> 00:03:27,220 Plus plus. 42 00:03:29,990 --> 00:03:32,720 Then we just copy over the following. 43 00:03:35,360 --> 00:03:36,050 After the loop. 44 00:03:36,050 --> 00:03:38,120 Print two new lines. 45 00:03:38,420 --> 00:03:39,830 Simple enough. 46 00:03:42,510 --> 00:03:44,490 And now we call the function for each ticket. 47 00:03:44,490 --> 00:03:46,410 So here we will say. 48 00:03:48,050 --> 00:03:51,200 Print ticket numbers for ticket one. 49 00:03:51,410 --> 00:03:54,560 Here we print the ticket numbers for a ticket to. 50 00:03:58,090 --> 00:03:58,780 All right. 51 00:04:05,650 --> 00:04:09,730 The variable ticket store is a reference that points to one array. 52 00:04:11,090 --> 00:04:16,850 The variable ticket to stores, another reference that points to a copy of the original array. 53 00:04:18,260 --> 00:04:18,589 Here. 54 00:04:18,589 --> 00:04:21,829 We're just going to print the numbers for the first ticket. 55 00:04:22,730 --> 00:04:25,790 And here we print the numbers for the second tickets. 56 00:04:27,850 --> 00:04:28,240 All right. 57 00:04:28,240 --> 00:04:29,020 That was all. 58 00:04:29,820 --> 00:04:34,980 The take home message is never set a raise directly equal to each other. 59 00:04:34,980 --> 00:04:42,990 Because if you realize that each variable stores a reference that points to an array by setting ticket 60 00:04:42,990 --> 00:04:44,590 to equal to ticket. 61 00:04:44,610 --> 00:04:47,520 All you're doing is copying over the reference. 62 00:04:47,520 --> 00:04:51,630 So now two variables share a reference to the same array. 63 00:04:52,020 --> 00:04:58,030 So by virtue of making changes through ticket to ticket is going to get affected as well. 64 00:04:58,050 --> 00:04:59,910 This is not good. 65 00:05:01,210 --> 00:05:05,860 Instead, what you should do is set this variable equal to a copy of the array. 66 00:05:06,190 --> 00:05:07,090 And that is all.