51
Module 201 Object Oriented Programming Lecture 10 - Arrays Len Shand

Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Embed Size (px)

Citation preview

Page 1: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Module 201 Object Oriented Programming

Lecture 10 - Arrays

Len Shand

Page 2: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Methods

Page 3: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Arrays One dimensional Multi dimensional

Page 4: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

The variables you have been working with so far have only been able to hold one value at a time. Your integer variables can only hold one number, and your strings one chunk of text. An array is a way to hold more than one variable at a time. Think of a lottery programme. If you’re just using single variables, you’d have to set up your lottery numbers like this:

lottery_number_1 = 1; lottery_number_2 = 2; lottery_number_3 = 3; lottery_number_4 = 4; etc

Instead of doing that, an array allows you to use just one identifying name that refers to lots of numbers.

Page 5: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

You set up an array like this:

int[] lottery_numbers;

Page 6: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

If your array needs to hold floating point numbers, you’d set your array up like this: float[] my_float_values; An array that needs to hold text would be set up like this: string[] my_strings

Page 7: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

The next thing you need to do is to tell C# how big your array will be. The size of an array is how many items it is going to hold. You do it like this: lottery_numbers = new int[49];

Page 8: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

So far, you have just set up the array, and created an array object. But the array doesn’t yet hold any values. (Well it does, because C# assigns some default values for you. In the case of int arrays, this will be just zeros. But they’re not your values!)

Page 9: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

To assign a value to an array, you use the square brackets again. Here’s the syntax: array_name[position_in_array] = array_value;

Page 10: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Here’s an example using our lottery numbers: lottery_numbers[0] = 1; lottery_numbers[1] = 2; lottery_numbers[2] = 3; lottery_numbers[3] = 4;

Page 11: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Another way to assign values in array is by using curly brackets. If you only have a few values going in to the array, you could set it up like this:

int[] lottery_numbers = new int[4] { 1, 2, 3, 4 };

Page 12: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Arrays come into their own with loops. The idea is that you can loop round each position in your array and access the values. We’ll try a programming example, this time.

Page 13: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

So start your C# software up, if you haven’t already, and create a new Windows Application. Add a button and a list box to your form. Double click your button to get at the code.

Page 14: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

For the first line, add code to clear the list box: private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); }

Page 15: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); int[] lottery_numbers; lottery_numbers = new int[4]; }

Page 16: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); int[] lottery_numbers; lottery_numbers = new int[4]; //We could also do: lottery_numbers = new int[4] {1.2.3.4}; lottery_numbers[0] = 1; lottery_numbers[1] = 2; lottery_numbers[2] = 3; lottery_numbers[3] = 4; }

Page 17: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

listBox1.Items.Add( lottery_numbers[0] ); listBox1.Items.Add( lottery_numbers[1] ); listBox1.Items.Add( lottery_numbers[2] ); listBox1.Items.Add( lottery_numbers[3] );

Page 18: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

So to get at the value in an array, you just use the array name and a position number, known as the index number: lottery_numbers[0]; This is enough to display what value is at this position in the array. Try it out. Add the list box code to your programme

Page 19: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); int[] lottery_numbers; lottery_numbers = new int[4]; lottery_numbers[0] = 1; lottery_numbers[1] = 2; lottery_numbers[2] = 3; lottery_numbers[3] = 4; listBox1.Items.Add( lottery_numbers[0] ); listBox1.Items.Add( lottery_numbers[1] ); listBox1.Items.Add( lottery_numbers[2] ); listBox1.Items.Add( lottery_numbers[3] ); }

Page 20: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

If you had a long list of numbers to display, you don’t really want to type them all out by hand! Instead, you can use a loop. Add the following loop to your code: for (int i = 0; i != (lottery_numbers.Length); i++) { listBox1.Items.Add( lottery_numbers[i] ); } Now delete all of your list box lines.

Page 21: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Loop Array i = 0 lottery_numbers[0]

i = 1 lottery_numbers[1]

i = 2 lottery_numbers[2]

i = 3 lottery_numbers[3]

The value in each position is then accessed, which for us was the numbers 1 to 4.

Page 22: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

i != (lottery_numbers.Length)

Length is a property of arrays that you can use. It refers to the number of items in your array. So we’re saying, “Keep looping while the value in i does not equal The Length of the array”.

Page 23: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

You can also use a loop to assign values to your arrays. In the code below, we’re using a loop to assign values to our lottery_numbers array:

for (int i = 0; i != (lottery_numbers.Length); i++) { lottery_numbers[i] = i + 1; listBox1.Items.Add(lottery_numbers[i]); }

Page 24: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

The only thing that has changed with our for loop is the addition of this line: lottery_numbers[i] = i + 1; The first time round the loop, the value in i will be zero. Which gives us this: lottery_numbers[0] = 0 + 1; The second time round the loop, the value in i will be 1. Which gives us this: lottery_numbers[1] = 1 + 1;

Page 25: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

What we are doing is manipulating the index number (the one in square brackets). By using the value of a loop variable, it gives you a powerful way to assign values to arrays. Previously, we did this to assign 4 numbers to our array: lottery_numbers[0] = 1; lottery_numbers[1] = 2; lottery_numbers[2] = 3; lottery_numbers[3] = 4;

Page 26: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

But if we need 49 numbers, that would be a lot of typing. Contrast that to the following code:

private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); int[] lottery_numbers; lottery_numbers = new int[49]; for (int i = 0; i != (lottery_numbers.Length); i++) { lottery_numbers[i] = i + 1; listBox1.Items.Add(lottery_numbers[i]); } } }

Page 27: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

As an exercise, halt your programme and change the index number of the array from 49 to 1000. Run your programme and test it out. What you’ve done is to set up an array and fill it with a thousand values!

Page 28: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

The size of an array refers to how many items it holds. You’ve seen that to set the size of an array, you do this:

int[] someNumbers; someNumbers = new int[10];

Page 29: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Sometimes, you just don’t know how big the array needs to be. Think of a programme that pulls information from a database. You want to loop round and check how many of your customers still owe you money. You’ve decided to hold all the data in an array. But how big does the array need to be? You can’t set a fixed size before the programme runs, simply because the number of people owing you money could change. It could be low one month, and high the next.

Page 30: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

To solve the problem, you can set the array size at runtime. This would mean, for example, setting the array size after a button is clicked.

Page 31: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

To see how it works, add another button to your form, along with a text box. Your form should then look something like this:

Page 32: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Double click the new button and enter the following code:

int aNumber = int.Parse(textBox1.Text); int[] arraySize; arraySize = new int[aNumber];

Page 33: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Add the following loop to your code, which just assigns values to the array, and places them in the list box on your form:

for (int i = 0; i != (arraySize.Length); i++) { arraySize[i] = i + 1; listBox1.Items.Add(arraySize[i]); } Run your programme and click your button.

Page 34: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Now delete the 5 and type a different number. You should see the number from 1 to whatever number you’ve just typed.

Page 35: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

This technique can be used whenever your programme needs to get its array size at runtime – assign to a variable, and use this variable between the square brackets of your array.

Page 36: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

To get some more practice with arrays, add another button to your form. Set the text to be this: Exercise: Times Table

Page 37: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Now double click the button and add the following code: private void button3_Click(object sender, EventArgs e) { listBox1.Items.Clear(); int[] arrayTimes; arrayTimes = new int[10]; int times = int.Parse(textBox1.Text); for (int i = 1; i != (arrayTimes.Length); i++) { arrayTimes[i] = i * times; listBox1.Items.Add(times + " times " + i + " = " + arrayTimes[i]); } }

Page 38: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,
Page 39: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

The first item in the array, arrayTimes[0], doesn't get used - why is this? Amend the code so that the times table from 1 to 10 displays in the list box, not 1 to 9

Page 40: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Your arrays don’t have to be single lists of items, as though they a were a column in a spreadsheet: you can have arrays with lots of columns and rows. These are called Multi-Dimensional arrays.

Page 41: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Column 1

Array Position 0 10

Array Position 1 20

Array Position 2 30

Array Position 3 40

Array Position 4 50

Page 42: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Column 0 Column 1 Column 2

Array Position 0 10 100 1000

Array Position 1 20 200 2000

Array Position 2 30 300 3000

Array Position 3 40 400 4000

Array Position 4 50 500 5000

Page 43: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

So if you wanted to get at the value of 2000 in the table above, this would be at array position 1 in column 2 (1, 2). Likewise, the value 400 is at position 3, 1.

Page 44: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

int[,] arrayTimes; You then need a number either side of the comma: arrayTimes = new int[5, 3]; The first digit is the number of Positions in the array; the second digit is the number of Columns in the array.

Page 45: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Filling up a 2-dimensional array can be quite tricky because you have to use loops inside of loops!

Page 46: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,
Page 47: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

arrayTimes[i, j] = mult; mult = mult * 10; NOW Add This Line: listBox1.Items.Add("arrayPos = " + arrayRows + "," + arrayCols + " val = " + (mult /10));

Page 48: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

Notice the two for loops in the code above, one inside of the other. The first loop is setting the value of the Rows (array Positions), and the second loop is setting the value of the Columns.

Page 49: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

For the adventurous, add another button to your form. Enter the code above. Now add a second double for loop, and print out the array values in your list box.

Page 50: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,
Page 51: Module 201 Object Oriented Programming Lecture 10 - …wiki.hct.ac.uk/_media/computing/fdsc/fdsclecture10.pdflottery_number_3 = 3; lottery_number_4 = 4; etc Instead of doing that,

String Arrays and text Collections