180 likes | 367 Views
Arrays. Computer Programming 2. Arrays. What is an array? An array is a “container” that holds more than one value whereas a variable can only hold one value. Each value in the array is called an element . Each element of the array must be the same data type. . Arrays.
E N D
Arrays Computer Programming 2
Arrays • What is an array? • An array is a “container” that holds more than one value whereas a variable can only hold one value. • Each value in the array is called an element. • Each element of the array must be the same data type.
Arrays Note that you are not restricted to primitive data types (int, double, char…). You can create arrays of structs, enums, or classes. Arrays are reference types – An array “variable” refers to an array instance. The size of the array does not have to be a constant; it can be set at runtime.
Declaring an Array • An array must be declared before it can be used. • You can declare an array one of two ways. • int[] intArray= new int[5]; • int[] intArray;intArray = new int[5]; • The index of the first element is always zero. • This array will have five elements. intArray 0 2 3 4 1
Declaring an Array You can also declare an array and set the size later.int size = Convert.toInt16(txtSize.Text);int [] nums = new int[size]; orint [] nums;int size = Convert.ToInt16(txtSize.Text);nums = new int[size];
Declaring & Initializing an Array • In the previous slide we declared an array but did not give the elements any values. • The array type determines the default value if the array is not given values when initialized. • String – null • Numeric – 0 • Boolean - false
Adding Values to an Array • To modify the statement to assign values when initialized: int[] intArray = new int[5] {1, 2, 3, 4, 5}; • We can also add values like this: intArray[0]= 1; intArray[1] = 2; • We can also use a loop if we are putting successive values.
Adding Values with a Loop int[] intArray= new int[5]; int i =2; int j=0; for (i = 0; i< intArray.Length; i++) { j = Convert.ToInt16(Interaction.InputBox ("Enter a Number.")); intArray[i] = j; } Note: Remember to use the InputBox from Visual Basic, you need to add the reference to Visual Basic (Project -> Add Reference) and add the following statement at the top of your code: using Microsoft.VisualBasic;
Pulling Data from An Array • So now your array has data in it! How do you display it to the user? Use a loop of course! • Consider the following example: for (int x = 0; x < intArray.Length; x++) { MessageBox.Show(Convert.ToString(intArray[x])); } • Length is a method that will give the length (number of elements) of any declared array.
Pulling Data from An Array Note: In C# we must convert data types. The Text property of the MessageBox takes a data type of String- so we must convert our Integer array to display it properly.
The Foreach Loop • There is a special type of loop just for arrays! The foreach. • This special loop’s main use is pulling data from arrays. foreach(int i in intArray) { MessageBox.Show(Convert.ToString(i)); }
Sorting An Array • Sorting data in an array is simple: Array.Sort(intArray); • This will sort the array from smallest to largest. • A string array would be sorted A-Z.
Sorting a String Array string[] strArray = new string[] {"apple", "orange", "banana", "pineapple", "pear"}; Array.Sort(strArray); foreach(string k in strArray) { lstFruits.Items.Add(k); }
Changing the Order of an Array We can flip the array (last element becomes the first) using the Reverse method. Array.Reverse(strArray); Using our string array - the new output is shown.
Sample Program Write a program that accepts three numbers and shows them to the user smallest to largest. Use textboxes for input and labels for output.
Solution private void btnSort_Click(object sender, EventArgs e) { int[] numArray = new int[3]; try { numArray[0] = Convert.ToInt32(txtNum1.Text); numArray[1] = Convert.ToInt32(txtNum2.Text); numArray[2] = Convert.ToInt32(txtNum3.Text); } catch (FormatException x) { MessageBox.Show("Enter only numbers"); return; } Array.Sort(numArray); lblNum1.Text = Convert.ToString(numArray[0]); lblNum2.Text = Convert.ToString(numArray[1]); lblNum3.Text = Convert.ToString(numArray[2]); }
Arrays • In this presentation you learned about one-dimension arrays. • Arrays are powerful tools that can be used to simplify programs. • However, arrays do have some limitations. • All elements in an array must have the same data types. • The size of the array cannot be changed.
More Information For more information about the Array class:http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx