ProgrammingMethodology-Lecture16.pdf
(
60 KB
)
Pobierz
Programming Methodology-Lecture16
Instructor (Mehran Sahami):
All righty. Welcome back to yet another fun-filled,
exciting day of CS106A.
So it’s the day before the midterm. This is kind of like, you know, the calm before – well
I shouldn’t even say the calm before the storm. It’s a probably a lot more calm for me
than it is for you. But hopefully you’re getting prepared for the midterm, so a few
announcements before we start.
There’s one handout. You still have sections this week, so sections will happening after
the midterm. The section problem is the handout for this week. The midterm is tomorrow
night and hopefully you should already know this by now, but just one more time just to
drive the point home: midterm tomorrow night, Tuesday, October 30th, 7:00 to 8:30 p.m.
in Cresby auditorium.
If you had emailed me before and let me know that you had a conflict with the midterm,
you should’ve already gotten an email from me this weekend letting you know about the
alternate exam, what time it was, what the instructions are for the alternate exam. So if
you think you emailed me about a conflict and you never heard from me this weekend,
please let me know right away, like right after class.
Right now, if you heard this and you think you have a conflict and you didn’t get an
email from me this weekend, you should be panicking because I think I’ve reached
everyone who said they have a conflict. And invariably, as you can imagine, there wasn’t
just one time that worked for everyone, so we ended up doing a little bit of gymnastics to
make it work, but hopefully everyone has a time they can actually make.
If you’re an SEPD student, email me by 7:00 p.m. tonight if you plan on taking the exam
at your site remotely. If I don’t get email from you by 7:00 p.m. tonight, I will just
assume that you are coming into campus for the regular exam, which is perfectly fine. If
you want to send me an email verifying that you’re coming in for the regular exam, that’s
great too.
I always love to hear from the SEPD students, but you’re not required to but you are
required to send me email if you’re gonna take the exam at your site. Otherwise, I won’t
know to send the exam to your site. So send me email by 7:00 tonight letting me know if
you’re taking it remotely, and let me know who your site administrator is, their name and
email. That’s critical because your site administrator will be administering the exam to
you.
All righty. So any questions about anything before we dive into our next great topic? All
right, let’s get started on our next great topic then. And tonight – today I should say, I
think it’s tonight because like the lights are on; it’s actually daytime. So today, there is a
whole new concept that we’re gonna be getting into called an array. Okay?
And all an array is, is basically a way to keep track of a whole bunch of information at
once. So what we’ve done so far in class is like when you wanted to keep track of
information you had some variable, and you had like an int, or you had a double, or you
had a g-oval, or whatever it was. And it was you kept track of some small piece of
information, but in the real world, really want you want to do is keep track of a whole
bunch of information.
Like say you’re gonna take your midterm, we would want to keep track of all of your
midterm scores. And it would be a real pain, for example, to say int Midterm 1, int
Midterm 2, int Midterm 3. You can just see how monotonous it is. And by the time I got
to –int Midterm 150 you’d be ready to kill, but we couldn’t stop then because we’d have
to go up to like int Midterm 325. Right? And so we just don’t want to do that and
computers are really good at dealing with lots of information. So the way they allow us to
keep track of a whole bunch of information at once is something called an array.
And so the two key concepts for an array is that an array is ordered, so it has some natural
ordering, and that means we can refer to the elements of the array using some index
based on that ordering, and it’s also homogeneous, which means that everything in the
array that we store is of the same type. So we can have an array, for example, that stores
integers, and we could say, “Hey, we want an array that stores 500 integers.” But it’s only
gonna store integers; it’s not gonna store a couple integers and a couple doubles and
some strings. All the types are the same so it’s homogeneous. Okay?
So how do we actually create one of these things?
We’ll erase some announcements. We’ll slide this puppy over here. It’s actually the first
time I’m sliding boards around. And the reason why I always hesitate to slide boards
around is all it takes is one really nasty time to get your finger crushed by a board, and
you never want to slide boards around again because you can’t write for a couple days
and it’s real fun to do class, especially for you with the left hand.
All right, so how do we actually declare an array? First, we specify the type of the thing
we want to have in that array. Then we have an open bracket and closed bracket. That’s
gonna indicate to the computer that this is an array of this type int. Then we give it a
name, so let’s just call this – I’m gonna call it My ARR, for My Array. Okay?
And then what we need to do is basically tell the machine, “Hey, create some space for
the array for me.” Okay? So the way we specify that is we use our friend new, then we
say the name of the type again, which may seem a little bit redundant because we just
said it over here. But the reason why we have to say it over here again is because when
you tell the machine, “Hey, I want you to setup some space for some integers, and the
place I’m assigning that to is an array of integers.” Okay? So these two types over here
actually generally need to match, but you need to have that redundancy.
And then the thing that we specify over here is we actually specify what the size is of our
array. So for example, we could have five. So what this is actually giving to us is it’s
giving us an array of five integers. Okay? And the way you can actually think about this
is an array is basically like a big list that has a bunch of cells in it, or what we refer to as
the elements of that array. Okay?
So this is my array, My ARR, and it’s spaced for five integers. And the way we refer to
these particular integers, or the elements of the array, and each one of these boxes hold
one integer, is they’re indexed because this is an ordered collection. Right? It’s
homogeneous because they’re all integers, it’s ordered, and it’s indexed starting from
zero. Because as computer scientists, we always start counting from zero.
So if I ask for an array of five elements, they’re actually indexed from zero up to the size
of the array minus one. [Inaudible]. Just like a string, right, like the characters in a string,
where zero up to length of the string minus one. Same idea with an array.
And each one of these things in it is storing an integer. And when you create a new array,
the values actually in the array get initialized. They get initialized to whatever the default
is for that type. What does that mean? Well, it means for things like integers, the default
value is zero. For things like Booleans, the default value if false.
And there’s a whole list of what the default values are, but we’re gonna just stick with
integers right now, so all these things start with the value zero in them. One of the
niceties, Java gives you the arrays in this life. So if you’ve worked with other languages,
like C or C++, it’s one of those things you have to just let go because the syntax is
different, the initialization is different, let go of your old C ways and just adopt the
happiness that is Java. Okay?
So how do I refer to these individual elements? The way I refer to the individual elements
is through the indexes. Okay? So I could say something like My ARR zero; and I put this
inside square brackets, so it’s square brackets over here and square brackets over here.
And what this is now referring to is this particular cell, which stores one integer. Which
means I can use that just like would use a variable of type int anywhere else in my
program. I can assign to it, I can use them in use them in expressions, whatever the case
may be.
So if I say my array equals five, what it’s actually doing is just sticking the value five into
this cell. Okay? So any questions about that? Hopefully fairly straightforward? All right.
So the thing you want to think about for these arrays, and what makes them kind of
funky, is first of all I get a whole bunch of values very quickly just by specifying the size
of the array. The other thing is that this little index that’s in here doesn’t necessarily have
to be a constant value like zero. I could actually put a variable in there.
So I could, for example, have a for-loop that goes through all the elements in my array:
for int I equals zero. I, in this case, I’m just gonna size is less than five, which is the size
but we’ll get to how we actually want to deal with that in just second. I-plus-plus and
then inside here I’d say my array sub I which is that particular I element. Maybe I want to
read this, for example, from the user: equals read int, and I’ll just, you know, put the little
question mark to ask the user for a value.
And so what this does is it goes through and essentially asks the user for five integer
values, and stores them in the indexes of the array from zero up to four. Okay? So one
quick loop does all the work, right? If I have 300 values, I change this to a 300, I change
this to a 300. I’d be good to go, that’s all I need to change in my program. So that’s kinda
the power of arrays. Okay?
So the more general form of arrays, this is how we might actually use them and in this
case, I’m just thinking about integer arrays. The more general form of thinking about
arrays is sort of like this.
Ooh, a lot of chalk dust.
You’ll notice here, we’ll just map to kind of general form. This is the type that you want
to specify. Okay? Then we have open bracket, close bracket, your angle, then you have
the name of your array. Then you say equals new, and you specify the same type again,
and end size brackets you have the size, so that’s kind of the general form.
So we could have an array of doubles, right? We could have an array of Booleans. We
could have an array of strings, for example, by saying string bracket-bracket. I’ll call this
s-list for string list equals new string. Oh, let’s say I want 100 strings. Okay? Life is
pretty easy. All right? So any questions about that? All right. Hopefully this is all fairly
straightforward.
Now the other thing that’s kinda easy is one – or that I should mention is you can have, as
you might notice, a string is an object. I could have an array, for example, of g-ovals, or
g-rects. So I could actually say something like g-oval, maybe is my type, and then I’m
gonna have an array of these things. And I’ll call this, you know, my circles because I’m
just gonna have a whole bunch of these ovals that maybe represent circles. And then I’ll
say new g-oval. And here’s where things get a little funky. I say new g-oval, and then
let’s say I wanna have ten of these things, I put a ten.
Notice this looks different, even though I use the new it’s different than the syntax for
creating one new oval. Right? If I wanted to create one new oval, I would say something
like, I’ll put it down here, g-oval, and I’ll just call this O for oval, equals new g-oval. And
then I could give it the parameters for the constructor, like some original X, Y location
and some size, maybe 100-comma-100.
What this is doing is creating one object of type g-oval and assigning it to the value O.
What this is doing is creating space for ten g-ovals, none of which have been created yet.
Okay? So what does that actually mean? Let me make this a little bit smaller so I don't
have to draw ten boxes; I’m gonna make this four, okay, but same effect.
When I do this declaration, what I now have is this thing called circles, which is an array.
It’s an array that’s got four cells in it. Okay? And each one of these cells is basically
gonna be able to store a g-oval. But each one of these cells right now doesn’t actually
have a g-oval in it. Why: because I haven’t created a new g-oval in each one of those
cells. Each one of these cells, or elements, all it has in it is the ability to store a g-oval.
It’s kind of like if you just said g-oval, and I’ll call this empty. Okay? What happens
when I say g-oval empty? I get this thing called empty over here, which is space for a g-
oval but I haven’t done new on it yet which means this thing doesn’t actually point to any
object that is a g-oval. Its value, for example, is null. Right? There’s no actual g-oval
there. I just say, “Hey, get me the box that’s gonna store a g-oval in it,” but I’m not
actually putting any g-oval in it yet.
So when I do this declaration, I’m essentially doing the same thing as this except rather
than getting one box, I’m getting for boxes. So each one of these boxes to begin with has
a null in it, okay? So I have space to store four g-ovals but I have not yet constructed the
g-ovals. So if I actually want to construct the g-ovals, I’m just gonna make use of this
little line right here. I could say, for example, circles sub zero equals new g-oval. And
what this will now do is say, “Hey, this is circle sub zero over here. Go and create some
new oval and stick it in that box.” So now, it actually has an oval to deal with. Okay?
Or if you want to think about it from the memory point of view, what really is happening
is this is some pointer somewhere that points off to some piece of memory that’s storing
the information for an oval. Okay?
So any questions about that? That’s a critical distinction between creating the array,
which is just creating the space for essentially these pointers or references to objects, and
actually creating the object. Which you still need to do, you can just create objects in
each one of the individual elements of the array if you want to. But if you just create the
array, you don’t actually get space for the objects themselves, you just get space for the
references to the objects. Much in the same way you do when you just declare a variable
of some object type.
Uh huh?
Student:
Is there a more efficient way [inaudible] an array [inaudible] spaces to
[inaudible]?
Instructor (Mehran Sahami):
To create new ovals, for example?
Student:
Yeah, [inaudible].
Instructor (Mehran Sahami):
Absolutely. So let me erase this and I will just put in one
extra line up here four int I equals zero, I less than, in this case, four I-plus-plus circle
sub-I equals new g-oval. So that’s the beauty of arrays, right, is the fact that you can use
them with objects just in the same way you could do them with ints. And so, you can go
Plik z chomika:
m_r_k
Inne pliki z tego folderu:
ProgrammingMethodology-Lecture01.html
(61 KB)
ProgrammingMethodology-Lecture01.pdf
(63 KB)
ProgrammingMethodology-Lecture11.html
(61 KB)
ProgrammingMethodology-Lecture04.pdf
(61 KB)
ProgrammingMethodology-Lecture03.pdf
(66 KB)
Inne foldery tego chomika:
Zgłoś jeśli
naruszono regulamin