1 / 18

Arrays

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.

lucita
Télécharger la présentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays Computer Programming 2

  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.

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

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

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

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

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

  8. 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;

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

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

  11. 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)); }

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

  13. 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); }

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

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

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

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

  18. More Information For more information about the Array class:http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

More Related