1 / 19

Array and ArrayList

Array and ArrayList. ISYS 350. Array. An array allows you to store a group of items of the same type together. Processing a large number of items in an array is easier than processing a large number of items stored in separate variables. Declaring a Array.

dinah
Download Presentation

Array and ArrayList

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. Array and ArrayList ISYS 350

  2. Array • An array allows you to store a group of items of the same type together. • Processing a large number of items in an array is easier than processing a large number of items stored in separate variables.

  3. Declaring a Array • Declare an array in one statement: • Type[] arrayName= new type[array size]; • Ex: • string[]empName = new string[3]; • Double[] intRate = new intRate[6];

  4. Array Elements • Array elements are indexed from 0 to array size – 1. • Each element can be accessed by its index: • arrayName[index] • Ex: • empName[0] • intRate[2]

  5. Array Initialization • With the declaration statement: • string[] empName = new string[3] { "Peter", "Paul", "Mary" }; • double[] intRate = new double[6] { .03, .04, .05, .06, .07, .08 }; • Initialize each element separately: • empName[0] = "Peter"; • empName[1] = "Paul"; • empName[2] = "Mary";

  6. Accessing Array Elements with a for loop intarrayIndex; for (arrayIndex = 0; arrayIndex <= 2; ++arrayIndex) { MessageBox.Show(empName[arrayIndex].ToString()); } • Using array’s Length property: for (arrayIndex = 0; arrayIndex <= empName.Length-1; ++arrayIndex) { MessageBox.Show(empName[arrayIndex].ToString()); } Note: Length - 1

  7. Example: Compute the sum and average of numbers in an array double[] myGPAs = new double[5] { 2.5, 3.2, 3.4, 2.9, 3.6 }; double sumGPA=0, avgGPA; for (inti = 0; i <= myGPAs.Length - 1; ++i) { sumGPA += myGPAs[i]; } avgGPA = sumGPA / myGPAs.Length; MessageBox.Show("Average GPA is: " + avgGPA);

  8. Using Array’s Methods and Length property • Sum(), Average(), Max(), Min(); double[] myGPAs = new double[5] { 2.5, 3.2, 3.4, 2.9, 3.6 }; double avgGPA, maxGPA, minGPA; avgGPA = myGPAs.Average(); maxGPA = myGPAs.Max(); minGPA=myGPAs.Min(); MessageBox.Show(“The size of array is: " + myGPAs.Length);

  9. Create a Loan Payment Form

  10. Data Binding: Binding an array to a control’sDataSource property • A string array to store rates with “%”: • string[] strRate = new string[6] { "3%", "4%", "5%", "6%", "7%", "8%" }; • Bind the array to a listbox: • listBox1.DataSource = strRate; • A parallel array to store the numerical rates: • double[] intRate = new double[6] { .03, .04, .05, .06, .07, .08 }; • Use listboxselectedIndex to access the rate: • intRate[listBox1.SelectedIndex]

  11. Code Example double loan, rate, term, payment; loan = double.Parse(textBox1.Text); rate = intRate[listBox1.SelectedIndex]; if (radioButton1.Checked) term = 15; else term = 30; payment = Financial.Pmt(rate / 12, term * 12, -loan); MessageBox.Show(payment.ToString("c"));

  12. ArrayList • An arrayList allows you to store a group of objects together. • Objects stored in an arraylist may not be the same type of object. • ArrayList resizes dynamically. As elements are added, it grows in capacity to accommodate them. • Numerical data stored in an arrayList needs to be casted.

  13. ArrayList Property and Methods • ArrayList • Count • Add • Clear

  14. Declaring an arrayList: 1. Must add this reference: using System.Collections;2. The Data Type of ArrayList Members Can Be Different ArrayListdemoArrayList = new ArrayList(); intmyInt=10; double myDouble=12.34; string myString="Hello"; demoArrayList.Add(myInt); demoArrayList.Add(myDouble); demoArrayList.Add(myString); for (inti =0;i<demoArrayList.Count;i++) { MessageBox.Show(demoArrayList[i].GetType().ToString()); }

  15. foreach Loop • The foreach statement repeats a group of embedded statements for each element in an array or each object in an arrayList

  16. foreach loop example foreach (object x in demoArrayList) { MessageBox.Show(x.GetType().ToString()); MessageBox.Show(x.ToString()); }

  17. DataBinding and parallel arrayList • Must add this reference: • using System.Collections; • Declare arrayListwith initial values • Ex: • ArrayListstrRateList = new ArrayList() { "3%", "4%", "5%", "6%", "7%", "8%" }; • ArrayListrateList = new ArrayList() { .03, .04, .05, .06, .07, .08 }; • New element can be added: • strRateList.Add("9%"); • rateList.Add(.09); • Number of elements: • rateList.Count

  18. Binding an arrayList to a control:Control’s DataSource property • A string arrayList to store rates with “%”: • ArrayListstrRateList = new ArrayList() { "3%", "4%", "5%", "6%", "7%", "8%" }; • Bind the array to a listbox: • listBox1.DataSource = strRateList; • A parallel arrayList to store the numerical rates: • ArrayListrateList = new ArrayList() { .03, .04, .05, .06, .07, .08 }; • Use listboxselectedIndex to access the rate: rate = (double)rateList[listBox1.SelectedIndex]; Note: Need the (double) cast operator.

  19. Code Example double loan, rate, term, payment; loan = double.Parse(textBox1.Text); rate = (double)rateList[listBox1.SelectedIndex]; if (radioButton1.Checked) term = 15; else term = 30; payment = Financial.Pmt(rate / 12, term * 12, -loan); MessageBox.Show(payment.ToString("c"));

More Related