Hello, in this episode we're going to be looking at arrays and lists, which are both ways of
storing a collection of variables of the same type.
Let's start with arrays: Imagine we want to store a number of different
Instead of creating a new variable for each one: string name1 = "Fred"; string name2 = "George";
and so on, we can rather create just one string array variable, which we do just like creating
a normal variable, except that after the type we place a pair of square brackets.
If we want, we can directly assign a collection of values to the array by having a pair of
curly brackets, inside which are all the elements, separated by commas.
Somewhere in our code we can now access any element in the array by writing "possibleNames",
followed by a pair of square brackets, inside which goes the index of the element we wish
Now there are 4 elements in this particular array, but arrays are indexed starting at
zero, meaning that the first element has an index of zero, the second an index of 1, and
If we try access index 4, or greater, hoever, we will get an IndexOutOfBounds exception.
Now lets say we want to pick a random name from this array for our character.
The first step would be to generate a random number between 0 and 3.
So if we're working in Unity, we can simply use the Random.Range method, and pass in 0
as our min value, and possibleNames.Length as our max value.
The length of our array is 4, but remember that the random.range method returns a value
that is >= to the min value, and < the max value; which means we are actually getting
a value between 0 and 3 like we want.
So, now that we have our random index, we can write string characterName = possiblesNames,
with an index of randomNameIndex.
Ok, let's look at another example.
This time I'll create an array of integers, which I'll call powersOfTwo.
Say we just want to store the first 5 -- well we've already seen one way of assigning these,
so lets look at another.
We can set the array equal to a new int array with a size of 5, at which point all 5 elements
in the array will be equal to 0, since that's the default integer value.
Somewhere in our code we can then write powersOfTwo = 2; powersOfTwo = 4; and so on...
Much smarter though, would be to use a loop to initialize all of these, and we'll be seeing
an example of this in the next episode on loops.
Note that we could also leave the initialization out at the top here; but then we'd still have
to define the array's size before we can use it.
Once we've initialized the array's size, we cannot change it, except by setting the variable
equal to a new array with a new size, which of course will destroy all the elements currently
inside the array.
Now arrays don't have to have just a single dimension.
Say we want a 2dimensional integer array to keep track of the pieces in some board game.
The syntax for creating this would like so: ... The size of each dimension is separated
by a comma -- so here I've create a 3x3 board.
A single comma is also placed between the square brackets at the declaration, indicating
that there is space for 2 dimensions.
We could now visualize the array as looking like so, with all elements currently initialized
to their default value of zero.
The indices work how you'd probably expect, with the first index determining the row,
and the second index determining the column.
So in some method, if we wanted to set the value of the bottom left square to 1, for
example, that would be as simple as writing board[2,0] = 1; Now perhaps the game requires
a starting configuration of pieces, with different pieces obviously represented by different
Well the syntax for this is the same as a one dimensional array, but now the entries
for each new row are surrounded by curly brackets as well.
To make this easier to read, we could separate each row onto a new line, which gives us a
nice visual representation of the array;
You're not limited to just two dimensions, by the way; for more dimensions, simply add
All right, let's now take a look at lists.
Say we want to do something like keep track of the names of every character that our player
kills so that we can display them all at the end of the game.
We might think to make a string array called slainEnemyNames, but unlike the previous examples,
the size of the array is uncertain, because we don't know how many enemies the player
will end up killing.
To solve this problem, we can make use of the List class, which is in the System.Collections.Generic
Now to create a list of strings, we write List, followed by the type string inside angle
We then of course have the variable identifier, in other words, its name, and we can initialize
this to a new empty list.
Take note of the syntax for this, we have the new keyword, followed by List of type
string, and then a pair of parentheses to call the contructor of the List class.
Say we now have an OnEnemyKilled method, which supplies us with the name of the enemy.
We can add this name to the end of the list using the Lists Add method, -- so slainEnemyNames.Add,
and pass in the supplied name.
Accessing a certain element from a list uses the same syntax as with arrays: just the variable
name, followed by square brackets, with the index inside.
Just like arrays, lists are indexed starting at zero.
Recall that to get the size of an array, we could use the Length property.
You can keep track of the number of elements inside a list using the Count property.
Lists also have some other functionality that mere arrays do not.
For example you can use the Remove method to remove a given element from the list, you
can use the Contains() method to check if the list already contains a given element,
and theres even a Sort method; so if you have a list of numbers, calling sort will cause
them to be arranged in ascending order, and if you have a list of strings, they can be
We'll even look later at how you can instruct the sort method to follow your own custom
So since lists can do everying arrays can do, as well as change size, and they have
helpful methods for things like searching and sorting, you may be left wondering why
we even need arrays.
Well I think it's important to know that behind the scenes, a list is actually storing everything
inside an array.
Whenever the internal array gets filled up, the list has to create a new, bigger array
and copy all the existing elements over into that new array.
Purely for interest's sake, on the screen at the moment you can see a heavily simplified
version of the List class, just demonstrating how the array is resized as new items are
So in general, arrays are slightly faster that lists, although in most cases this difference
The real reason you might sometimes want to use an array over a list is if the collection
should logically have a fixed size.
For example representing a game board, or storing the items in a player's inventory.
In these cases the concept of "adding" an item doesn't really make sense; you only ever
want to insert an item at a specific index.
I should mention that its entirely possible to have a list of arrays, or an array of lists,
or a list of lists, or an array of arrays, or pretty much any configuration you can imagine.
I should also mention that arrays and lists are reference types.
So if I have an empty integer array, say with a size of 5; and I now create a second array,
and set it equal to the first array -- then, if I set one of the elements to 99 and print
out the value of that element in the other array, we'll see that it also now has a value
If this seems strange to you, please go back and watch episode 17.
Anyway, to recap: arrays are for storing collections of a fixed size, and can have one or more
Lists are slightly slower than arrays, but are able to resize themselves dynamically
as you add and remove elements, and also provide some helpful methods, such as Contains(),
Finally, arrays and lists are reference types.
That's everything for this video, until next time; cheers.