Let's talk arrays or collections of stuff in Swift, let's create one var my array is equal to two square
brackets and then whatever numbers you like in those square brackets, separated by commas.
So if I go to the next line and I type my ah, let's see what kind of thing it is.
My array of integers, so an array is a collection of the same kind of thing, in this case integers,
that's as big or as small as you want it to be, but it's a way of stacking together similar types of
variables that you don't want to create lots of variables for, obviously.
The interesting thing actually about arrays is that they are stored sequentially in memory with most
languages.
So if I want the value five and six, they actually sit next to each other, at least logically in the
memory, perhaps not physically.
I haven't looked into it that far, but they sit next to each other as far as memory addresses go.
OK, so that is an array, a collection of like minded items.
Now we can explicitly tell the system what kind of array we want so we can have vah and we can tell
it.
I want right now an array of type string and then I'm going to give it an array of strings.
Just two of them.
Hi there.
So there are two strings in that array, and we've told the system this is an array of strings, of
course, we could potentially do the same up top here.
We say this is an array of, you guessed it, integers.
And as long as it's getting what it wants, then we're all good if I put five point eight in here.
We should get a nice red arrow because I've tried to insert a double rainbow, five point eight is not
an integer.
Now, some languages would actually take that five point eight, drop the point eight and just give
you five, which is an awful idea as far as languages go.
Okay, so what else have we got on my notes here via Mayan's?
This is another way of declaring your array is equal to an array of integers.
Open and closed brackets, so in this case, there are zero elements in this array which are saying
just initialize it so it's ready for me to use because I don't have anything to put inside it just yet.
Now, how do you put things inside?
Well, we go to my aunt's dot and you'll notice there are lots of functions we can use here.
We can append the number eight.
So now it adds the number eight.
We can have my aunts dot append the number two or one.
And if we run that.
We should get nothing.
Eight and then eight and one as the elements of this particular array.
How do we access the items in the array?
Well, that is very straightforward.
We have my int.
We open the square brackets and we give it the index, so the first element in an array is always element
zero.
Everything in programming is zero based indexing.
You start counting at zero.
So if I run, this zero will be the element of eight.
Now, of course, that doesn't spit out an array that's giving us an integer because we've gone away,
we've dove into the array already and we're pulling out that individual item, whatever type it is,
in this case, an integer.
Right.
So your homework, nice and simple, perhaps.
Create a three element array of numbers, multiply all of them.
Now the tasks are starting to get a little bit harder.
If you're new to all of this, so sit down and just think about how you'd go through this.
Don't worry about the code.
What I'd like you to do is think conceptually about it, writing a piece of paper on your iPad, on
your phone, how you think these should be laid out.
A lot of code, a lot of problems that we need to solve in code are solved by logical thinking, not
by knowing code.
Yes, it's partly knowing code, but most of the work is done in just thinking logically, first of
all.
So give that a go and I'll see you in the next one.