You want to write a program that keeps track of the heights of different plants in your garden. You could do this as a series of variables, but those would become difficult to work with really fast.
// height in inches int plantOne = 4; int plantTwo = 3; int plantThree = 6;
Luckily, C# provides us with a built-in solution to easily keep track of lots of different pieces of information. Data structures are formats designed to store larger amounts of information in an organized fashion.
An array is one very basic data structure. Programmers use arrays as a container to store multiple pieces of information that relate to each other in some way. Like a list of the presidents of the United States, types of cheeses in alphabetical order, and the finishing positions of runners in a race.
What makes arrays special is that they order our data in a specific, linear sequence. Since our values are kept in order, it allows us to easily find the information we’re looking for; otherwise, we’d have a huge jumbled mess of data! Which means, rather than having to create multiple variables and retrieve information using a variable name, we can use a single array name and retrieve it using its location in that array.
You’re already familiar with strings - strings are arrays of characters. Just like we can use syntax to access parts of string, we can do so with arrays.
In this lesson, we’ll cover:
- How to build arrays in C#
- How to find the length of an array
- How to access array values
- How to edit arrays
- How to read documentation and use built-in methods
Instructions
Comic strips function in a similar way to lists. We can think of the strip as the list and each frame as a separate item in the list. The narrative that makes up a comic demonstrates that the items follow a specific order.
We create lists by adding items to an empty list. Right now we have a bunch of comic frames, but they’re not in any order and our comic strip is empty. Complete the comic strip by placing the frames in the correct order. Here’s how the story should go:
- Codey plants a seed
- Codey waters the seed
- Codey waits
- A sprout grows