If you've been watching these videos about arrays... whether or not I mentioned it, I probably did, but I don't specifically recall.
You would have noticed this. An array is a fixed size. When we declare an array. When we make an array.
When we say I'm going to have an array of
Bubbles, right. When I say I'm going to have an array of bubbles. I have to say it is a new
array of Bubbles and I have to specify the number of bubbles
I want to have. I have to say are there going to be ten bubbles, 100 bubbles, a thousand bubbles.
That number... that is the size of the array. The size of that array can never change. If later in the program
I suddenly want 200 bubbles. I can't just add an extra hundred bubbles there.
I could delete the array and make a new array and do all sorts of crazy stuff, but an array is a fixed size.
So we... this is obviously a severe limitation. I mean there are plenty of reasons
why a fixed size is perfectly good for you.
If you are making some sort of a game, and it has 10 players.
Then you just need an array of 10 players. 10 scores.
10 player names. Right. You know, there's not suddenly ever going to be 12 players or 9 players. It's always a 10 player game.
There's many instances where an array of fixed size is totally what you need and that's great.
But there are also lots of scenarios where you want to start with no bubbles on the screen.
And then a moment happens and a few bubbles come out and then another moment happens, and now there's six bubbles,
and there's 12, and then there's 15, and then suddenly all the bubbles go away, and there's no bubbles anymore.
I don't know why I'm talking about this strange world of bubbles
but you can imagine lots of different scenarios where you want to have this flexible number of elements on the screen.
We need to develop strategies for doing this and there are many.
So um there's there's a couple that I'm going to just sort of briefly gloss over and point you
information about. For one if you look through the processing reference under array, you'll see that there are a whole set of functions.
There's one called append.
I don't remember what the other ones are called. It's funny cuz this is what happend to me the last time I did this video.
There's a lot of functions and what these functions allow you to do is
manipulate the size of the array and you might choose someday to use those functions. I encourage you to go look up.
Sorry have a cough I encourage you go to look up those functions in the reference to see how they work. See if they apply
to you, but let me just make something very very clear.
Let's say we're going to use the function append and we have an array of three elements.
happening in the magical computerland is not suddenly that we're appending another element to the end of this array
and it becomes bigger.
That's what's going that sort of like the result that we want but what Processing really has to do in order to
make this larger array...
Is it has to make a new array that has four spots in it. It has to take the first three
copy them over and then now we have a new spot to add an additional bubble.
So while this works and the append function is something you could use,
it's not exactly the most efficient and also the syntax for append is slightly awkward and a little bit weird.
So I don't love use...
I love that these functions exist, and I'm very happy about them, but I don't love using them
and I personally don't use them that often. The the solution that I generally use the most often is something called an array list.
You can also find the array list in the Processing reference. You can find an example
under Topics, Advanced Data. I will link to that below.
About how that uses an arraylist for a flexible size... a flexibly a
list of things that has flexible size. You can start with nothing add a bunch, delete a bunch. An array is great.
It's an object. It has functions.
You say... you can say remove, add
get, add a whole bunch of things. Use all sort of stuff you can do with an arraylist.
I think I just said array earlier when I meant arraylist.
I meant arraylist. So an arraylist is something I encourage you to look into and there will be a
video that covers the an arraylist more specifically after this one at some points or a link to a video anyway.....
Whatever yep, don't worry. I got you covered. Eventually I'll
do something about an arraylist.
But what I want to do is actually show you a solution that I think is useful in some scenarios, but also
just it helps you think about how... how you can manage....
it just helps you practice and kind of use arrays more in an interesting way.
I was doing so well, but this is you know whatever. I'm doing the best I can.
This is my second attempt, and we're just gonna keep going through it, okay.
So what let's.... let's give ourselves a scenario to work with. Here's a scenario... We have this..... we have this example...
Which we notice we have 100 bubbles.
We make a hundred bubbles. They have some random size.
And then we call these functions and all these bubbles are floating up on the screen.
So let's... let's, let's build in this scenario for ourselves a little exercise for ourselves.
What if when this program starts what we want is for there to be no bubbles on the screen...
and every time I click the mouse...
a new bubble appears. How would we do that?
Well one thing that you can always do with an array. Is that you could always say...
You know what I'm gonna make an array of a million bubbles. Is that a million? A million a million bubbles
and then that way sometimes if I want to use 10
or if I want to use 200 for one is 300 I can just use part of the array.
So let's leave ourselves at 100 and let's think about what we would do. First of all let me show you something.
Look at this loop down here. int i equals 0. i is less than bubbles dot length.
What does this loop do?
i is 0 than 1 then 2 then 3 then 4 then 5. It's all the way up to 99. Its counting through every element of
the array. What if I just said...
Even though there's a hundred bubbles in that array,
I'm only looping through the first 10. Right? If I say zero, how many do I have on the screen?
I'm not looping through any of them. If I say 50...
What's happening? I only have 50 of them. Now of course if I said
150. We've got a new problem on our hands...
Array index out of bounds exception.
Array index out of bounds exception. I think... oooh and I think you probably... I probably mentioned this before...
But this is the error you get if you try to access an element that doesn't exist. When your array has 10 spots and you
go for number 15... That index is out of bounds.
So watch out for that error.
So we do have a limit here when there's only a hundred
but we notice we can change how many were actually actively using at any given point. So...
What am I doing here? I keep changing this number manually and running it again.
What is what should that indicate to you? That might indicate to you
we could use a variable. So what if I had a variable that I called total and I set that variable to zero...
I put total down here. Well now when I run it... Zero elements.
When I run it with ten. Ten bubbles... well, this is a variable now.
What could I do? I could say void mousepressed()...
total equals total plus one.
Look at this no bubbles.
Let's hope this works. Click the mouse, one bubble. Click the mouse two bubbles. Click the mouse three bubbles. Click the mouse four bubbles. Click,
click, click, click, click, click, click, click, click, click, click, click, click and I'm adding bubbles.
So even though we've made an array of hundred bubbles if we
impose some logic where we only use a certain amount of the array, then more of the array
and then what could? I do I could add...
The camera just went off, but you know what I'm doing. I'm gonna turn it back on.
If I add a key pressed this one's not going very well. total equals total minus one, and I run this again.
What can I do?
I could say hey let me add a bunch of bubbles.
Let me delete those bubbles. So we can see here that we can manipulate
how much of that array that we're using and this is not such a bad strategy
And you know even though in arraylist is probably ultimately going to give us more flexibility more power
and just it's for more ease in manipulating the size of an array. This is a nice sort of
scenario to practice with and kind of get a sense of as an option in... in how you use an array.
Now... so, let me give you some thoughts if you want to take this a step further into an exercise.
Here's what I would say to you number one is what do you do when total gets to 100? Right, total equals total plus one?
There's still only 100 bubbles in that array. What do you do? Do you set total back to zero?
How can you keep reusing it? Think about what what scenario you would do there?
The other thing you might think about is how could this happen autonomously? So how could you just say every frame. There's like a
1% chance that a new bubble would appear could you pick a random number?
And test where that random number is and like what if I picked a random between 0 and 100 and if that random number is
Less than 20 does that mean there's a 20% chance?
I think I just gave the answer. But implement that. So you don't have to click the mouse to add bubbles.
What if it just happens every so often randomly so those are some two things you could try doing.
But mostly what I would say to you is
find your thing that you've been working on that has an array and just sort of play with the idea of only using part of
the array. Could you sort of turn objects in the array on and off? You know that's another thing we could do.
What if in the bubble object... here's something you could try. What if in the bubble object, you put... we put a boolean variable...
and I said active and it was false...
and then I have some if statements in there where I only do the stuff to the bubble if it's
active and what if when I click I sort of activate different bubbles. That could be a way that I could also
manage certain things to be there or not there at any given moment. So those are some things you could try some ideas.
Maybe I'll try to implement that and put it somewhere if it's not there
and you're come to this video email me and remind me but
but what I would say is um the next thing you should be looking at though is at some point is using an arraylist and
in the next video or oh I think I have a video already from then let me just say it. I
have a video from the Nature of Code series that I'm pretty sure
goes through an arraylist and what I'm going to do is link to that from from this video.
So you can go move on to that if you're interested in arraylist, and if that doesn't exist, then I'll make one. Okay
I've babbled for too long.
I hope this one was ok and I will see you
later. The next set of videos will just be I think the next set of is gonna make about images and pixels. Image processing.
Loading images from the camera all sorts of cool fun stuff that hopefully you will enjoy. Ok good. Bye.