Locked lesson.
About this lesson
In this lesson, we learn how to create and access Arrays.
Exercise files
Download this lesson’s related exercise files.
23 - Creating and Accessing Arrays.docx61.1 KB 23 - Creating and Accessing Arrays SOLUTION.docx
58 KB
Quick reference
Creating and Accessing Arrays
An array is like a variable that you can put more than one thing in.
When to use
Use them when you need to deal with more than one item.
Instructions
string[] names = new string[3];
names[0] = "John";
names[1] = "Mary";
names[2] = "Tim";
Console.WriteLine(names[0]); // outputs John
// Another way to add things to an array:
string[] names = new string[3] {"John", "Mary", "Tim"};
Hints & tips
- string[] names = new string[3];
- string[] names = new string[3] {"John", "Mary", "Tim"};
- names.Length // returns length of the 'names' array
- 00:04 In this video I want to start to talk about arrays.
- 00:06 And arrays are incredibly important with every kind of programming language
- 00:10 out there.
- 00:11 And C# is no different.
- 00:12 An array is, basically, like a variable, but
- 00:15 you could put more than one thing in it.
- 00:16 So, it's a bucket with many compartments.
- 00:18 Now, C# is a little different than some of the other modern programming languages.
- 00:22 You have to be very specific when you define and create an array.
- 00:25 You have to say how many things you're going to put into it.
- 00:27 That's not the case with other programming languages,
- 00:30 a lot of other programming languages like Python, and Ruby, and things like that.
- 00:34 So, let's just jump in, and let's create our first array.
- 00:37 So, it's very similar to creating a variable, we have to define it, and
- 00:41 initialize it.
- 00:42 And just like with a variable, we have to express the data type we want to use.
- 00:45 So, let's create an array of strings.
- 00:48 So, this is going to be a string, and then we use these square brackets.
- 00:52 That's what tells C#, hey, this is going to be an array.
- 00:54 And then, let's name it.
- 00:55 So, let's create an array of names like John, Bob, Tim, things like that.
- 01:00 So, we do that.
- 01:01 Now, we want to create a new string array.
- 01:04 And here we have to tell it how many things we're going to put in here.
- 01:09 Now, we don't always have to do that.
- 01:10 And I'll show you a way to get around that, in a minute.
- 01:11 because this is a little bit annoying.
- 01:14 But, starting out, we're going to do it this way.
- 01:16 So, new string[3].
- 01:18 And that's it.
- 01:19 Now we've defined it.
- 01:20 Now, there are several different ways to add items to an array.
- 01:23 The first is to just, very explicitly put them in.
- 01:27 Now, the thing about an array is, like I said,
- 01:29 you're putting multiple things into an array.
- 01:32 And each of those things has an index number.
- 01:34 And the first item that goes into an array is number 0.
- 01:38 So, the 0th item is the first item.
- 01:40 And that messes up a lot of new students.
- 01:43 Why don't we start with 1?
- 01:44 It's just one of these things, it's almost universally arrays start at 0.
- 01:47 So, we can call names, and then we can use our bracket and say, hey,
- 01:52 for the 0th item, make that John, right?
- 01:55 And then we can, similarly, say,
- 02:00 the first item will be Mary and
- 02:04 then names[2] will equal Tim, all right?
- 02:10 So now we have three things, three things in our ray.
- 02:15 1, 2, 3, the 0th item, the first item, 0th item is John.
- 02:20 The first item is Mary.
- 02:21 And the second item is Tim.
- 02:23 Now, again, I know this is going to throw off a lot of people,
- 02:26 the second item of three items, but it's still the last item.
- 02:29 Just sort of burn that into your brain.
- 02:31 So, here, we can access any item in an array just like we defined it,
- 02:36 by calling it.
- 02:37 So, we can say names[0], save this, run it.
- 02:43 And we get John, right?
- 02:45 Similarly, we could go names[2], save this, run it, and we get Tim, right?
- 02:52 We can try names[3], save this and run it, we're going to get an error.
- 02:59 All kinds of crazy things happening here, right?
- 03:03 The entire program has almost crashed, it's so angry with us.
- 03:07 So, we get out of there.
- 03:10 So, like I said, you have to be explicit with these things.
- 03:14 And I mentioned, this is one of the ways we can add things to our array,
- 03:17 very explicitly.
- 03:18 We can also come up here and use curly brackets, and
- 03:22 put the items into our array, right?
- 03:25 When we define it.
- 03:26 So, similarly, we could go John, you notice I'm using quotation
- 03:31 marks separated with a comma, go Mary, and here we can go Tim.
- 03:35 And this will do the exact same thing as this, but
- 03:39 it's a little neater, it's all on one line.
- 03:42 And it's nice so here if we call the first item
- 03:48 that's going to be Mary, 0, 1, 2, right?
- 03:53 Save this, run it, And we get Mary.
- 03:58 One more very quick fun thing.
- 04:01 We can find out how many things are in our array, if we don't necessarily know,
- 04:05 if we forgot, if we need to know what the last item is by calling names.Length.
- 04:10 And this will return something a little confusing.
- 04:12 If we run this, you'll see it's returning 3.
- 04:16 So, that's a little confusing because the last item is the second item, right?
- 04:22 But we know there are three items because that's how we defined it.
- 04:24 So, that's what it's returning.
- 04:26 So, we could be really kind of cagey here.
- 04:28 And let's say we wanted to return the last item, but
- 04:30 we forget how many items are in there.
- 04:32 So, we could call names, and then instead of passing in a number here,
- 04:38 we could call names.Length-1.
- 04:41 because, remember, the length is 3, but the last item is 2.
- 04:46 So, we have to do -1 on there.
- 04:49 So, this should return Tim.
- 04:50 Save this, and run it, sure enough, we get Tim.
- 04:54 So that is a very quick introduction to arrays.
- 04:57 You can use arrays for all sorts of things in your coding career.
- 05:00 They're super important, they're super useful, and they're just very prevalent.
- 05:03 They're always going to be there.
- 05:05 So, we're going to spend the next few videos talking a little bit more about
- 05:08 arrays in depth, and you'll become very familiar with them.
- 05:11 We used strings here, but you could similarly use integers.
- 05:15 And, very quickly, we could go int[] nums = new int.
- 05:22 Let's say we want to put 3 things in there.
- 05:25 The difference between strings and nums, we don't use quotation marks.
- 05:28 So we could just go 1, 2, and 3.
- 05:31 This will, again, let's go nums and nums, this will return the last item,
- 05:37 which should be 3, the number 3, we need a semi colon there.
- 05:43 Now, if we save this and run it, We get 3.
- 05:48 So, like I said, those are arrays, you can use strings, you can use int,
- 05:51 you can use any of your data types at all.
- 05:53 And that's all there is to it.
- 05:54 In the next video, we'll look at updating arrays.
Lesson notes are only available for subscribers.