Have one welcome back in this lesson we're going to talk about one of the most fundamental aspects of
any programming language and that is a use of collections of collections such as arrays or sets or dictionaries.
They help us put objects of the same nature or different type into a container.
Now why would we need to do that.
We would need to put objects of the same type in an array so that we can have an order between them.
Imagine you want to have the days of the week as store somber as opposed to saying we want to string
for Monday that a string for Tuesday resting thing for Wednesday.
So on and so forth I could put all of them into one object only and then go to the other and find the
ones that I need.
So let's jump in to exclude And let's get started.
So we get a better understanding of how it works when I go through ex-cult in here I'm going to start
a new project me close that one first and then I start your project my voice command line tool is called
arrays sex.
Scott It doesn't matter what you call it anxiety.
So just go ahead and start our very first array.
As always I'm going to clear this thought I'm going to get myself is better for my thing.
So the first thing I want to do is I want to declare an array.
I'm going to say and this array it is called are days array and that is declaring an array.
Now the next thing I have to do is and this is quite important.
I have to both allocate and initialize an array.
And this is a topic we haven't really seen much in the past and we will talk about it much much more
details in the future.
So the next thing is allocation and initialization which is done like this.
Désirée is A.R.T. allocate and initialized with some objects without those objects were the only objects
that they want to put in my array.
Well one of them is Monday.
The other one is Tuesday and then the next one is Wednesday Wednesday.
And let's say I only needed for four days of the week and whenever you put in the middle that means
this is the end of every simply because arrays don't have a limit.
You can put as many objects as you want into them.
And when we are initializing them we need a way of saying well this is the end of the entry.
So I put that on a very common practice is the stacking your objects like this.
So I'm going to say that that kind of you know you can see all of them together.
Now let's see what we have done.
We have declared an array and we are located then initialize the lock and initialized with these objects.
That's essentially like doing it definition of it.
So we had a declaration and we have that definition and now we have an array with all the objects.
Let's see what's that order.
So the very first object it is not what we call index 0.
Then the next object is that index 1 and you can imagine as we go along this will become index.
This will become index 3 so on and so forth.
So we have so many many objects in our array and they are all or that which is quite an important aspect
of an array being ordered.
So how did we get access to these objects.
Let's go ahead and say I wanted preens the element that is indexed too.
So I'm going to say and it's like perience or whatever you find.
Or let's say object not in the next two years is just like what I've done before.
And here I'm going to say désirée object at index 2 for instance.
So this line will now print the object that index to is a space and then if you look for any object
that it finds in here.
So in here we're going to run this.
Right we're going to say days are a find object that likes to run it and see what we get if we get Wednesday
it means everything we've done has worked properly.
It's like this object of next to is Wednesday.
Now the next question is how will I know how many objects are in my array.
I could do something like that.
I could say and like anyway as I'm going to stay there.
All right.
And then because that integer objects in my array obviously now and the slug is expecting us to feed
it some sort of an integer value and that integer value is a day's array that count.
So that's essentially going to return the count of the array for us.
We do get a warning in here and that's primarily because days that Chowhound is not of the type integer.
Let's have a look at it.
Count.
It is actually and this unsigned integer.
So to do to properly format that I could actually say l you.
So instead of printing integer go on preens l you for us if you don't remember this you can obviously
refer to that Apple document that we had a few lessons ago.
And also you could actually rely on ex-coach to fix it for you.
So in this case I would just go to this morning and it says well it's an unsigned long so fix it and
it says well you have to make it l you.
And that's pretty much does the work for us.
Now I'm going to go ahead and fix it manually again because I don't want to force it over there and
let's print this let's see what we get.
And now it says object that index 2 is a state.
Obviously one thing hasn't changed.
There are four objects in my array.
0 1 2 3.
Those are the indexes on 1 2 3 4.
Those are the numbers of them.
Now the next question that happens is what if I wanted to add the Friday here.
Is there a way for me to add another day to the day's array.
And as you can imagine the answer is No.
Simply because we're using an innocent array.
If you remember from our analysis string lesson if you want to be able to modify your values they have
to become mutable.
So go ahead and do that when I go in here and say instead of this array and this mutable array obviously
if it isn't in it's mutable.
That's my initialization.
It has to be code in this mutable array as well.
And now I have eight days or eight which is in this immutable array and it has Monday Tuesday Wednesday
Thursday and I'm printing the counterfit and then I'm saying days array and objects and I'm going to
add Friday and what the object does.
It goes to the end of the array and adds one more object to it.
And if the prince the counterfeits.
Now I'm going to print it here one more time.
You will see that at the beginning there are four objects in my array that is here when we are the Friday.
I can't see that there are five objects in my area.
Now to be printed the Wednesday this is using the object that index method.
I want to quickly talk about the use of these methods on the dots you know operation and everything
in here even though we have a lesson for methods and functions it kind of helps us understand why we
are doing some of the things that we are doing.
So things such as an array or mutable array they have a built in method called object that index.
It means you use a square bracket the name of your object the name of your array and then you say object
that index and you feel that the index that you want gets back to you.
So what is this one.
What are we using at DOT operation.
We will see this in more detail when we talk about classes and objects but some methods such as count
they can be used both ways.
They can be used.
A dot count or they can be used as this day's array chart that is essentially called a dot operation.
This is called the method calling in here and I'm calling a method called count which returns the count
of an array in here and using a dot operation which accesses the property count of the houri.
They mostly exactly the same thing and we get the same result.
Not exactly the same result but we get the same return essentially.
There's also a much easier way for getting the different elements from the array.
So let's say you want to print the object that is at the end of my array.
So I'm going to go ahead and say and Aslaug in the end.
I'm going to say the last object is and it's going to be something.
What is that thing that thing is going to be days array object that index number right count now or
not.
So again you get my space is array does count.
Now here's the thing.
If you can imagine this are how it would work you should expect this application to crash.
And I will show you why and we'll run the application and it's going to crash and the crash report says
in the X5 beyond bounds 0 2 4.
So it's saying you're trying to access an element that doesn't exist.
But how is that possible.
I'm looking at the count.
The thing is currently we have 0 1 2 and 3 indexes.
So the last index that we can use is 3 even though our count is 4 after we have the Friday.
This is going to be 0 1 2 3 and 4.
So the indexes are 0 to 4 while the count is 5 essentially in here.
I'm saying désirée object that index 5 which simply doesn't exist is saying you only have indexes zero
to 4.
So if I want to find the last object I can say count minus 1.
And that's always the last object of an array.
And now it's as last object is Friday.
There is also an easier way for writing this.
I'm going to rewrite it again.
This is called using a subscript in a subscript.
We do this we say désirée and then we open brackets and we say say four.
So it will print the elements number four for us.
Of course this is a little bit different than this one because in this one we are doing it statically
we we're always looking at number four.
But here we are doing it dynamically and you're looking at the count minus one which happens to be four.
In this example but not always.
So if I want to I could actually grab this and put it there.
And if you want to be very specific and with much further you know practice you could actually do something
like this.
You could say in the last index his days array dot count minus 1.
So that is the last index.
This is the one that I want to see.
Days are the last index.
So let's print the.
So we got Friday again and again we got the same warming's saying that this guy is actually an unsigned
long while this guy is going to be an integer and you can let X gotta fix it for you if you want you
can force fix it yourself.
To force ourselves the thing you have to do is you have to come to your count and you have to say I'm
going to cast it into an integer.
Again don't worry about this exactly.
We'll talk more about casting in the future.
But casting essentially means taking one type of event and forcing it into another one.
So because these are that count wasn't the type that we expect that.
I said take it and convert it into an integer and put it into my integer of course.
Casting is a little bit of a risky process because you have to be sure the objects that can be converted
to each other.
For instance I know an unsigned integer can't simply be or sorry an unsigned long can be converted 20
integer but that's not the case between let's say an integer and a string.
So once the time comes we'll talk more about the casting and how we have to be certain about what we
are trying to combat.
So the next thing you want to talk about are sets and what's the difference between a set and an array.
Let's jump into Apple's own documentation on in here.
There is a picture that explains that for get the picture in the middle part.
And these are eight years or third set of objects that is 0 1 2 2 for ABC and a set is an on or that
set of objects a easy g b c something to keep in mind is that because in a sense or not or that you
cannot have the same object happened twice.
So all objects must be unique but in an array they don't have to be unique.
Now let's go back to school.
Let's quickly try that.
So in here I could actually have two copies of Wednesday.
You know it would be perfectly fine.
You might have a situation that your OP is dealing with.
I don't know a certain number of users on the same user happens to be twice maybe the same user has
commented twice.
And that's why there is two copies of it there.
So in an array that's perfectly fine because they're all there and this means they would get indexed
too.
Well this one would get indexed 3.
However when you deal with sets and this is how you do a set and I said to my set is an asset.
And in it the objects and the objects say Sunday and only say Monday for now.
There's only two of them.
And in this they actually have to be you have to be unique simply because there's no order between them.
And now why isn't that an order between them because when there is no or there disparity between them
which is hashing between them happens a lot faster.
So in principle if you have an if you have an array or if you have a collection of objects that the
order between them doesn't matter you can rely on sets if the order between them does not.
Then you have to use arrays for the most part.
We end up using arrays simply because the order matters to us from time to time.
We end up using things such as set which all that doesn't matter and the objects are unique.
Now you might be wondering if there is no or there how can I access different objects from set.
I can not do things such as my set objects that index something because that order doesn't exist and
that is true and I'm going to show you something that is coming up in a few lessons.
So don't worry overly If you don't completely follow me.
But here is how you do search within the set to find something.
So in a set we say for and it's a string.
Any in my set and then I'm going to say if any is equally strange and let's say I'm looking for Friday
I'm trying to find out if an object as unique as Friday exists and I'm going to say and it's like there
is a Friday or something to that effect.
Now don't worry about this format in particular.
This is a for loop which is a method of control flow and we'll talk about that in a few lessons in fact
in this I'm going to say find all objects of the type in a string within this array within the set.
So in my set go through them one other time this on and on and whatever is there if any of them is equal
to the value of Friday.
Then tell me that there is friday or do something with that phrase so on and so forth you will see a
lot more of this when we talk about the for loops on control.
In a few lessons.
But for now just keep in mind that if there are sets it means the objects are unique.
And also it means there is no order between them.
So finally before I close this first and there is two more things that they should talk about.
One of them is objects in an array.
Don't have to necessarily be of the same nature even though we usually try to do that.
But you could have an array such as this.
And as I train my combined array and that is going to be an array lock in object.
And one of my objects is a string.
And then I have another object that let me put all the brackets so I know there are no arrows going
up.
And then I have another object.
And this object is going to be R and this number number of its integer say 8.
Don't worry about what an innocent number is.
What does matter is that this is now a string.
This one is not this thing is a different type of number different type of file.
And that's perfectly fine.
You can have it another array and sometimes it happens when you're reading values from networks or whatnot
but when you're doing things on our own we try to maintain same type of value within an array.
Finally there is another way of writing arrays which is essentially C Vai and all the array of writing
arrays that doesn't offer us a lot of things that we could do it in this race or in it's notable arrays
or in a set.
But from time to time it's useful.
And here is how we do it.
I say it's my end just like how we have been defining and declaring integers but then we put two brackets
in front of it and we say for instance 5.
It means I want to have an array of integers with five elements.
You have to decide about the number of items in it to right away right at the declaration time.
When I say that is going to be one two three four and five.
So I have an array now with five elements in it.
Those are integers and the integers are these guys obviously in a situation like this.
I can't change one of them to be string.
I can't have something like this because then is going to give me an error saying well you said everything
in here is going to be an integer.
How come you're trying to force a string into it.
So that's something to keep in mind.
And also that you have to declare and define the number of objects in the array right at the very beginning
which kind of limits what you are trying to do.
But sometimes it might actually be useful.
So that the writing is very good.
We talk about arrays.
We talked about mutable things we thought about how to get objects that indexes we talked about subscript
mod to get access to objects.
We talked about getting the count of finery.
We talked about adding objects into an array.
Something you probably didn't talk about is removing an object and that's actually quite simple you
can say these array remove objects.
We can either say remove this a specific object say or remove object Friday which is going to remove
all instances of that.
Or you could say désirée remove object at index if you know where you want to remove.
I want to remove the object that is in the first ID.
It is zero indexing zero.
So that's another thing.
And finally we talked about casting some of stuff we made a set we talked about hope for finding objects
in a set.
You have to iterate through them using a for loop or a similar.
And at the end we spoke about having arrays with different types of Firebirds or values inside them
and a C way of making array which is an all the way with that in mind.
Let's move on to our next lesson.