Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
Welcome to Workbook 6.12 and this one you decide to play the lottery and you're feeling good about these
numbers.
I guess you had a dream about them or something, and now it's telling us use a raised copy of to copy
all the elements of ticket into ticket two and then change the third value to 54.
Now what I'm going to do actually is the exact opposite.
Instead of copying every value from ticket into ticket two, what I will just do is set ticket to equal
to ticket.
And if I were to change the value at index 2 to 54, what do you think would happen?
Let's visualize the runtime and find out.
So the variable ticket is zooming in.
You'll see that it stores a reference that points to an array.
If you set ticket to equal to ticket, all you're doing is copying over the reference.
So now take it two shares the same reference at ten, pointing to the exact same array.
So whatever changes I make through the ticket to variable will also affect ticket.
Isn't that crazy?
So this is the reference trap by setting ticket to equal to ticket.
All we're doing is copying over the reference that points to the exact same array.
That's no good.
What we need to do is instead of copying over the reference copy over all the values, which means we
can say erase copy of.
We're going to create a copy out of the original array and we're going to copy the full length of the
array.
All right, So if we try this again.
Ticket store is a reference that points to this array.
Ticket to.
Store is another reference that points to a copy of the original array.
So now if I change, the state of ticket to ticket remains unaffected.
And it's very nice that breakpoints allow us to visualize the fact that the variable store is a reference
that points to an array at ten points to one array at 12 points to another array.
Task number two, create a function that prints the ticket numbers.
All right, so the function name is print ticket numbers.
It's going to be void, but it will take an one parameter of type int So public, let me zoom out a
little.
Public static void print ticket numbers.
Event tickets.
First thing we do is use a loop to print the ticket numbers in one line.
So we create a loop that runs through the length of the ticket array.
I is smaller than ticket length.
Plus plus.
Then we just copy over the following.
After the loop.
Print two new lines.
Simple enough.
And now we call the function for each ticket.
So here we will say.
Print ticket numbers for ticket one.
Here we print the ticket numbers for a ticket to.
All right.
The variable ticket store is a reference that points to one array.
The variable ticket to stores, another reference that points to a copy of the original array.
Here.
We're just going to print the numbers for the first ticket.
And here we print the numbers for the second tickets.
All right.
That was all.
The take home message is never set a raise directly equal to each other.
Because if you realize that each variable stores a reference that points to an array by setting ticket
to equal to ticket.
All you're doing is copying over the reference.
So now two variables share a reference to the same array.
So by virtue of making changes through ticket to ticket is going to get affected as well.
This is not good.
Instead, what you should do is set this variable equal to a copy of the array.
And that is all.