Module 201 Object Oriented Programming Lecture 10 -...

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

Transcript of Module 201 Object Oriented Programming Lecture 10 -...

Module 201 Object Oriented Programming

Lecture 10 - Arrays

Len Shand

Methods

Arrays One dimensional Multi dimensional

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.

You set up an array like this:

int[] lottery_numbers;

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

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];

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!)

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

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

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 };

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.

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.

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

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

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; }

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

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

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] ); }

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.

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.

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”.

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]); }

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;

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;

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]); } } }

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!

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];

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.

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.

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

Double click the new button and enter the following code:

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

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.

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

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.

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

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]); } }

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

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.

Column 1

Array Position 0 10

Array Position 1 20

Array Position 2 30

Array Position 3 40

Array Position 4 50

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

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.

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.

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

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

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.

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.

String Arrays and text Collections