Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: House Builder - November 19, 2018 :: The Game - December 14, 2018 :: Pong - December 17, 2018

Computer Programming I :: Lessons :: Arrays

Arrays

Array File CabinetA normal variable can hold only one thing at a time, but an array can hold more than one item. We compared a variable to a box, but we can think of an array as a file cabinet that can hold a single item in each drawer.

The different storage areas (or drawers in the analogy) of an array are known as an index. The first index of an array is known as index 0. It is important to remember that arrays always start with 0. So in the array above Peter Griffin would be in index 0, Homer Simpson is in index 1, and Fred Flintstone is in index 2. In all, the array can hold 3 items with indices 0 through 2.

An array is declared in the following way:

string[] cartoonDads = new string[3];

The main difference between an array declaration and a normal variable declaration is that you need to declare the size of an array. The number represents the number of items the array can hold. Note that the index numbers start at 0 so the example above would go from index 0 to index 2. Let's try an example to see how to store items in an array and read items from an array.

Constructors

A constructor is is a special method that creates a class. We will discuss exactly what a class is later in the term, but in the case of all of our programs so far the constructor we are worried about is the one that creates our form.

  1. Create a new project called "Arrays".
  2. Find three pictures from the internet and add them to your project in pictureboxes.
  3. View the code of your project.
  4. Type the following at the top of your code in the class:
PictureBox[] testArray = new PictureBox[3];

We are going to create an array of picture boxes. A picture box is a class, and you can declare variables that use a class as their type if the class is set up to be used in that way. Most form components can be used in variable declarations. Now let's make the constructor appear so we can fill the array with the names of our picture boxes.

public Form1()
{
  InitializeComponent();
}

The InitializeComponent() method is used to create your form. Removing this would prevent your form from loading. Underneath InitializeComponent() is the area you can use to type any code you want to run before your program starts. This is the area we will use to populate, or fill, our array with values.

  1. Underneath the InitializeComponent method call type the following:
testArray[0] = pictureBox1;
testArray[1] = pictureBox2; testArray[2] = pictureBox3;

Storing a value in an array is similar to storing a value in a variable, except you need to include the index in parenthesis that you would like to use. Your array now has the three PictureBoxes stored in its indices.

Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram