 Hello, it's time to go for today's mini lecture. And let me close off a couple of these things here, which I don't need. So I was grading some of the programs from earlier. Okay. So here we have an array of numbers and this is using a different form of initialization. Instead of saying we have a new array, we can give the numbers that we want in braces or we went over that yesterday. That's correct. Now let's say I want to print that out and I say system dot print line of numbers. And let's compile that and run it. And well that's not very enlightening. It is to some extent the square bracket means that this is an array. It means it's an int. And this at means this is its location in memory, which is nice to know, but not tremendously useful. If we want to see what's actually in the array, we need to run a loop and print out all the individual items. And I'm going to copy and paste this copy that already from the book. And I'm going to have to reinvent everything here. Okay, I've seen this a lot in some of the programs that I've been grading people have their methods right next to each other with no space in between. If you put one blank line in there, it makes things a lot more readable. And so that's what I would strongly suggest that you all do. By the way, this is one of the things that's in the extra exercises book, which the full book sort of glosses over is how do you pass an array. You can put numbers to a method. And the answer is the parameter for an array, you have to tell what data type it is. And then tell that it's an array by using the square brackets, and then give it some name and the book uses a which I'm not particularly happy with, but let's go with what the book has. What they'll then do is they will print a colon brace, excuse me. The first element of the array. Then starting at element number one up through the length of the array a dot length gives us how many items are in the array. And then we'll go I plus plus we'll print out a comma and a space. And then that element. And when we're done with the loop, we print a closing brace, which means we now have to call this method. We call print array. It says okay, you're going to have to give me an integer array. And well it says so happens I have an integer array right here. And numbers is going to be passed in as the value for a, and that should work rather nicely. And in fact if I run it. There I get my 1066 4711. A note about length here. If I have a string, and I want to find its length, I use the length method. So for strings, length is in method. I need the parentheses there and it gives me a nine. If I have an integer array. Numbers and let's put in. I say numbers dot length. So parentheses length is a property of the array. And this is a little bit of a contradiction. I won't say it's contradiction. It's an inconsistency. And it just bugs the heck out of me and I keep getting it mixed up all the time so I will not blame you if you get it mixed up sometime during your programming career. And numbers dot length gives me four. For strings, it's a method. The parentheses for arrays. It's a property. You don't use the parentheses. Don't blame me. I didn't make the decision. I'm just reporting it. Now the next thing the book talks about is copying arrays. So let's go here and let's save this as copying arrays dot Java. And this is something that's really important to understand. And this is what's going to be what happens when you copy an array to another variable. Does it copy the individual items or just the reference. And this is the thing that we better talk about right now. Things like int, boolean, char and double are what are called primitive values. If you look at the memory diagram. And I'm not sure if I can find one here quickly. Let's see if they have anything here. Okay, yeah, let's go here to variables, memory diagrams. So when I have a is assigned three and B is assigned five, the value is directly tied to the symbol for it. But with arrays, it's different. When we have an array, the name of the variable is a reference to some area of memory where we will find the data. So the data is not in the same place as the variable, like it is with primitives. String array, and any objects, which we will get too much later, are reference values. So this is incredibly important to understand. And I'm going to keep a guess of our column change the name of this copying arrays. So let's say I have here integer array. Copy becomes a numbers, and I assign this. And I do a print array of copy. I should get the same thing twice. And indeed, that's exactly what happens. However, let's look at the memory diagram for this sort of thing. Well, if I had a new double array of length three and a and I said B becomes a a and B because they are references, I now have two references to the same area of memory. In fact, I can prove this, by the way. Yeah, that's not going to work. And sure enough, they are both referring to the same area of memory. Because if I do something like this, if I say copy sub zero becomes a 999. And now I say system dot out dot is the print array of numbers. I will not get 1066 4711. Instead I'll get 99966 4711. Why, because I have a copy of the reference. They're both referring to the exact same area of memory. So when I change one of their content contents of that area of memory is immediately reflected in the other variable. This also is the case, by the way, when you pass an array to a method, you get a copy of the reference, not a copy of all the data items. Now what if I wanted a true copy of them so that they are truly a separate set of numbers. Here's one way to do it. I can say true. Let's call it separate copy. I'm going to make a new integer array. That's exactly the same length as numbers. And then I'm going to say for int I is zero, I less than numbers dot length, I plus plus. I'll take separate copy. Sub I becomes numbers sub I. What I have now done is I've taken this numbers of I and I've put it into a different area. In fact, if we do this, let's print this out to prove that I'm not just kidding you, because I created a brand new array in here in line 33 separate copy is not going to have the same reference as numbers will. And indeed, these are not the same reference anymore. That's great. So now if I say separate copy sub one becomes 888. If I now say, let's do a print array numbers. And that should give me the 999664711. And if I try printing array of separate copy. I'll get 9998884711. I keep hitting the print screen button for some reason, I don't know why. So this is a way to make a separate copy. You build a brand new array, and then take the numbers from the elements from the original array one at a time, and put them into this new area of memory. And sure enough numbers and separate are now different from one another. Let's make this a little bit more readable. Here's. There we go. Now everything is labeled nicely. So that's something you need to know. Again, arrays are reference values. They're not primitives. Now this means that when we pass something an array to a method, we can change the contents of the array. I'm going a little bit into what the extra book has at this point. So we're going to do here is we're going to pass an array to a method. And change the arrays values inside that method. So let's go here and we're going to say public static void cube elements. And we're going to have an integer array of data. Again, I can call it anything I want. They called it a I'm going to call it data. So what we're going to do is we're going to say for and I is zero I less than data dot length I plus plus. This is the nice thing about length I don't have to know how long the array is if I just go up to data dot length that guarantees that I'm going to hit all of the elements in the array. I can say data sub I becomes data sub I times data sub I times data. I think to return here, by the way, do this raise that look there. And now what I'm going to say here is I'm going to say, cube the elements in numbers, and then I'll print array numbers. Let me put a comment in here to explain what's what's going on here. Because we pass a reference. To the array. As our parameter, any changes made to the contents of the array are reflected in the original array. Some of you may be remembering this thing about call by value when I said that you always get a copy and you can never affect the original. And you may be thinking, well, this, this just isn't true. The explanation for it is really long. I think I need to write it up at some point as a PDF for y'all. All I'll say is call by value is still working. We're not changing the reference. We're changing the thing it's referring to. So I can't change the original thing. I can't change the original reference. The reference is to some area out in memory, and I can change that area of memory as much as I want. You might be thinking, well, gosh, that's a little bit dangerous, isn't it, because you're taking your original and all of a sudden your original data is gone forever. Yeah, that's an interesting thing to note. One of the drawbacks of using references is that because you have a reference to the original and numbers is a reference to these numbers in memory, and data is going to be another reference to this exact same place. So a change to one is a change to the other one. That's the bad news. The good news is all I'm passing is a reference. Today I had a hundred numbers in my array. Instead of having to pass a copy of all hundred numbers, I'm passing one reference, which is like four bytes. And again, back in the day when Java was invented and memory was at a premium, copying a thousand eight byte or a thousand four byte integers, that's four thousand bytes, that's a lot of memory. Copying one four byte reference, that's like free. Now some people again are not happy with the idea that changes to one make changes to the original. They would rather say, you know what, we're not back in 1995 anymore. Now we have more memory. What I'd like to do is I'd like to take this array here numbers and get back a new array and just not fool around with the original so that the original doesn't get touched. And I'll show you how that works. Let's call this return new array dot Java. So we're going to pass an array method. Create a brand new array with the results of our computation. And then return that as a new array, leaving the original. Which was our parameter untouched. And let's call this return new array. We'll call this new cubes. We're still going to get an integer array. But now instead of returned having no return value, we're going to give back a new array of integers. Parameter is an array of integers. And the return value will be a new different. What we're now going to do is we're going to create an array called result. And it's going to be a new integer array that's exactly the same length as our original array. Because we made a new integer array, this result, and this data are not in the same place in memory there and to totally different places. So any changes I make to result will leave data totally unaffected. I'm still going to take that from the data array, each element and cube it and then I'm going to put it into my result array. And then I'll return the result. Now, because I am returning a result. What I have to do here is I have to say let's make an integer array called cubed elements. And that's going to be the new cubes of numbers. So we're going to take the numbers array, we're going to give it to new cubes, it'll return us a new array of integers which goes into there. And we can print cube numbers, cubed elements. Let's call it cube numbers. That's a better name. We can see that our original array is untouched. Why, because we never put data on the left hand side of an assignment. And then the question comes up, okay, which way is better. Is it better to change an array in place, like we did here, without returning a new copy. Or is it better to create a brand new array and leave the original untouched. This is simply my opinion. Okay, this is not a law. This is not a guideline. This is my opinion and my preference is to always return a new array and don't touch the original. You still have to know about how to change something in place because that may come up on a test or it may come up during an assignment. So you want to know about both techniques. The question is, well, when would you ever use this one because, you know, it can screw up your original rate. The answer is on your regular desktop computer probably not a big deal. But there may be places like you're writing something for a microcontroller where you really do have limited memory. And in that case, you will want to do, okay, I'm going to, I'll eat the cost of wiping out my original array rather than copy rather than making a brand new array in order to save memory. So it depends on the situation, but 99% of the time, you are not going to be bound to buy limited memory. And so, again, this is just my preference. It's not a rule that you are required to follow. My strong recommendation is that you write your methods so that they return new arrays, leaving the originals untouched. I had to pause the recording there. I was deciding whether it was going to be confession time and go into a lot more detail about when I do and when I don't make copies, but that would take us far away from the topic at hand, which is arrays. So I'm not going to get into that. There is something though that I do want to get into. And let's go here and let's grab our template. And let's save this as let's say array product. And for this program is given an array of integers. I mean, they could use double as well there. Right a method that returns the product of all the elements in the array. So we're going to have our integer numbers and we're going to do our typical. Should I do the sum of the product I'll do the product what the heck. And the way I can do this is I can say okay my integer result start at one not zero because I started zero and I do multiplication things are not going to go well. So again for index I can use index or I zero index less than data dot length index plus plus product becomes product times data sub index. Hello, I called it result. Alright, so how about being consistent. And then I can have here, our integer product becomes get product of numbers. I'm going to set up 106 647 and 11 because I can't figure out my well it doesn't matter if I'll get and then system that out about print f product of the elements is percent D. Period percent and product. Oh, lovely. Okay. Absolutely this is one thing you'll may have noticed is that when you're running in genie or most integrated development environments. blank lines tend to accumulate towards the bottom of the source file, and you want to get rid of them you don't want to have a source file it has 50 blank lines at the bottom that's just really annoying. compile that. And let's run it. And 3412, you know what for a comma in there. That makes me feel better. And let's check that to make sure that's right by the way, 10 times 66 times 47 times 11. If 341220 cool it worked. Now you may have noticed that the index the only reason I'm using it is because I need to get the items from the array. The index itself is not part of the I'm not using index to do anything special. There's a way to do something that will make this a little bit more compact. Let's call this array product to that Java. And let's see what does the book call it I want to make sure I'm using the same as a book they enhanced for loop. Thank you. What I can say is for each integer item. In my data array. And this variable can be anything I could call it value I could call it be I could call it X, but for each item in the data array result becomes result times item. You give a variable data type which has to be the same data type as your array. You're able to hold each item one at a time, and then give the name of the array. And this is a compact way Java will go through each element in the data array one at a time. Again, I don't need the index for anything. So why should I use it. If I compile this and how about renaming it properly. It still works out great. So this is very convenient. This is the enhanced for loop. Now, if you do need the index for something, then you have, you can't use the. I won't say you can't use it, then you are better off not using the enhanced for loop. What would be an example of that. We want to calculate the weighted sum of a list of integers. For example, if the array is five, seven, 12. The sum will be five times one plus seven times two plus 12 times three. Essentially what we're doing is we're giving each number the weight by its position, starting counting at one instead of zero. All this weighted sum. And save it as weighted some Java. All right, in this case we're going to have an int sum which starts off at zero. And then we're going to have to say for indexes zero index less than data dot length index plus plus. We're going to say some becomes some plus data sub index times index plus one. Because remember when index is zero, we don't want to multiply by zero, we want to multiply by one. This method calculates the weighted sum by adding each item times its position in the array, starting at one. This method requires the index as part of the calculation. So using the enhanced for loop, which doesn't give us an index would be not a great idea. I could still do it, but it would be really ugly. One example where an enhanced for loop, I can't easily use it because the index is part of the calculation. And by the way, let's, well, this is interesting. Oh, yeah, how about returning the sum. That's the first problem. Let's compile this. Yeah, it's not called data it's called numbers. Wow, okay. I guess it's getting late because I'm making all these weird little errors. Oh, it's compile. Dang. Oh, yeah, that's an array. Oh, how many errors can I make in one simple program. Yay, finally. Okay. And the weighted sum is 55. And is that correct? Well, let's see. Five plus 14 is well, let's go to go the cake. I'm too lazy. So I have five plus 14 plus 36, which is 55. Yep. Okay, cool. So that pretty much takes care of what I wanted to talk about tonight. So now we've known how to traverse an array we've known how to display it. Oh, there's one other way to display an array by the way. And that is let's go back here to return to array. Another thing that we can do is this instead of saying print array numbers. Let's save this as instead of having to write this ourselves. What we can do is we can import Java dot util dot arrays. Notice the S at the end. And then instead of this, we can say from the line of that, plus arrays dot to string of numbers. And then here we can also say, plus arrays dot to string of cube numbers. We don't need that anymore. And let's do a print LN. The original array is still plus arrays dot to string of numbers. And you'll notice that that prints all the elements in the array, except to print them in square brackets instead of curly braces. Oh well, if you can live with that that's perfectly okay. So that's another way to print things out without having to write a method. So let's use the two string method in the arrays class, but you have to import Java dot util dot arrays to make that work. Now that's it for tonight.