9.2: Declare, Initialize, and Use an Array - Processing Tutorial

Sharing buttons:

Okay. We're back. Okay, jumping jacks everybody. Okay, um, so.

We kind of had a little bit of a little trip up there before but here we are, we've returned.

This is the moment. This is the moment.

We're going to actually use an array in our code.

So here's the thing... Remember a while back?

Video something... Link to it down here in the magic linking internet system. That um....

We learned about a few steps when we wanted to have a variable in our program. We first had to declare...

the variable. That involved the name and the type. Then we had to...


the variable.

That involved giving it its

initial value. Then, you know, we wanted to use the variable. I suppose

we said, maybe at the time, so that's kind of optional.

But what's the point of declaring and initializing a variable if you're not going to use it. So...

We actually what we want to now look at doing is the same thing with an array. And we kind of did that here.


We declared the array. It's an array called numbers of type integer.

And it's an array because we put the brackets in here.

We initialized it by giving it this list of values enclosed inside of curly brackets and separated by commas.

We didn't get to use yet. That's what I want to get to in this particular...

video. The thing is... this was kind of, like, I don't know,

maybe this was a mistake for me to actually even show this to you. Because this is a little bit misleading.

This is reasonable. I mean this is correct. It works syntactically

it's correct. But this is kind of somewhat rarely used. When we are about to make a list of bubbles...

do we know... are we going to actually set if we want a thousand of them. Are we gonna have a

thousand somehow bubble objects separated by commas. If we need ten thousand numbers are we going to list ten thousand numbers separated by commas?

No, there are some instances where you might hard code in the values of an array.

But for the most part... what we actually need is an additional step.

Which I'm going to put as 1a here for arrays. Which is to create the array.

And by create I mean how


is it? Or maybe a better way of standing that would be: How



Right? How many spots are in this array? 1, 2, 3, 4, 5, 6. This array has a length of

6. There are six

values in this arra. So a more common way of

doing this...

of crea... of declaring the array. Involves also creating it...

Like so...

This is....

Okay, we're good. We're good. We're good. We're good. We're good. This is step one. Now step one a.

We're going to say nums

equals a new


with seven spots.

Boy, so you know here's the thing. I in some....

I know. It's like and and and you could make some you could really complain now if you want to. You'd be totally within your right.

There's a lot of confusing syntax here.

Let's simplify this even further. Steps 1 and 1a most commonly you would do in one fell swoop.

I didn't... just like we say int equals 7. So let's rewrite this.

Sometimes it would be nice if there was a face right there

and I could see whether you were getting this or not because today

I feel like a little bit of spinning in the wind here.

But I think I'm just not... my train of thought isn't as clear. Ok, int


equals new int

7. Boy 7? Let's look pick a different number. Let's say there's 10 spots.

Okay, what have we done this is all steps 1 and 1a.

Look how redundant and weird this looks, but let's try to unpack it for a moment.

This is the name of my variable, of my array. That's the name of my array.

This is the type of the array.

What type is it? It's going to have integers in it this is indicating

it's an array and not just a single variable. It's a list. And

this over here is saying how long.

There are going to be ten spots. Now, it seems redundant.

What am I... if I were to translate this into English, I would say I hereby declare that

I will have an integer array called nums and

there I will create ten integers. Right, so, we why... it seems redundant that we have to say int twice.

But that's just how it works.

We need to create a list of 10 integers and store it in a variable of type array of integers.

That's what's going on. So this hopefully will become more fluid to you as you start to use it over and over again.

But the question is: What are... what's in the array right now? The moment that this happens....

we now have

1, 2, 3, 4, 5, 6, 7, 8; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

I should've been able to do that correctly. Okay? So what's actually in there?

A whole lot of zeros.

Because the default value for an integer is 0. Just like if I had only setup() here. int x

semicolon. If this is all I say int x semicolon the default value for that variable is going to be a 0.

So this moment. We're a... we're only here at step 1a we now need to get to step 2 - Initialize.

Step 3 - Use.

Well when we want to initialize a variable, we would say x equals 0. Or when we wanted to

use a variable we would say... you know line

x, y, you know, 10, 10 or something, and we're using that variable there.

So how do we use the array?

Here's the moment where we stop talking about the whole array.

We only really talk about the whole array at the moment of its birth. We say hey, I'm going to make this array

it's going to have ten spots in it. Go forth into the world and be an array.

Now we want to talk about individual elements of the array and the elements of the array are referred to by index number.

There are 10 things and you might say to yourself: "Okay...

Thing number 1, thing number 2, thing number 3, thing number 4, 5, 6, 7, 8, 9, 10. This is not the case.

The indices of an array start at zero.

0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

There are ten spots and

the index values range from zero to nine.

This is...

very very crucial. This is a very crucial detail of

everything you're going to do. The fact that you always want to count from zero.

Integer zero and nums zero. Nums index 1, nums index 2.

So if I wanted to initialize one of them... I would say nums...

Index 4


132. What's just happened there? Let's go look at our array. Let's find index 4 and let's set its value to 132.

Let's say I wanted to initialize the first one. nums index zero equals negative three.

What happens there? I've put negative three here. Let's say I want to use one of them. I'm going to draw a point.

From nums index 1 and nums index 2.

What have I just done? I've drawn something at the point zero, zero. Because those are the values currently in the array. So

actually using an array is

identical to using a single variable x. x equals zero. Line x, y. The only thing is now we have to refer to

which element of that array.


remember we have to declare the array.

We have to give it a type and we have to specify an array with the brackets.

We have to say how long that array is going to be. After that we just need to

assign values to individual elements,

and we can use the individual elements; all refer to their index. The index

values start at 0 and end

at the length of the array minus 1. So an array with a 100 elements would have index values from 0 to 99.

An array with 14 elements would have index values from 0 to 13.

Ok, so, we're nearing the end of this video, but I want to take all of this knowledge...

and apply it... Well actually, you know what, I don't want to do it.

I'm gonna give you this as an exercise, and then I'm gonna.... we're gonna start this in the next video.

No, yes.

Shoot! It would really help if I planned this in advance. See the thing is maybe what I'll do is

just look at all these videos and then go back and redo them all and I'll sort of have planned in advance.

I'm probably just incapable of that anyway.

Yeah, I think we were out of time, so let me actually give you this as an exercise.

So this is kind of a bit of a challenging one.

What I would say to you is looking at this syntax. What if you replaced int

with bubble?

Could you turn this sketch here instead of having Bubble b1, b2; to have an array called bubbles? And

we're certainly going to get to that. That's something we're going to do, I think, you know, it's funny

I think we're gonna do that in the next video.