Download presentation
Presentation is loading. Please wait.
1
Array 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 double[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
int arrayIndex; 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 (int i = 0; i <= myGPAs.Length - 1; ++i) { sumGPA += myGPAs[i]; } avgGPA = sumGPA / myGPAs.Length; MessageBox.Show("Average GPA is: " + avgGPA);
8
foreach Loop The foreach statement repeats a group of embedded statements for each element in an array.
9
foreach loop example double[] myGPAs = new double[5] { 2.5, 3.2, 3.4, 2.9, 3.6 }; double sumGPA = 0, avgGPA; foreach (int i in myGPAs) { sumGPA += i; } avgGPA = sumGPA / myGPAs.Length; MessageBox.Show("Average GPA is: " + avgGPA);
10
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);
11
Create a Loan Payment Form
12
If rates are stored in an array
Method 1: Using a loop to create the Listbox with rates in the Form Load event string[] strRate = new string[6] { "3%", "4%", "5%", "6%", "7%", "8%" }; for(int i=0;i<=strRate.Length-1;i++) { listBox1.Items.Add(strRate[i]); }
13
Method 2: Data Binding Binding an array to a control’s DataSource 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; Using the Form Load event
14
Data Binding Example private void Form1_Load(object sender, EventArgs e) { string[] strRate = new string[6] { "3%", "4%", "5%", "6%", "7%", "8%" }; listBox1.DataSource = strRate; }
15
Parallel Array Example
A parallel array to store the numerical rates: double[] intRate = new double[6] { .03, .04, .05, .06, .07, .08 }; Use listbox selectedIndex to access the rate: intRate[listBox1.SelectedIndex]
16
Code Example double[] intRate = new double[6] { .03, .04, .05, .06, .07, .08 }; 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); textBox2.Text = payment.ToString("c");
17
Sort an Array Array Class: Sort Method Example:
Sort array elements in increasing order Example: Array.Sort(myGPAs);
18
Exercise: Weighted Avg of three exams= 60%. highest score +30%
Exercise: Weighted Avg of three exams= 60%*highest score +30%*2nd highest score +10%*lowest score Method 1: You may sort the array of exam scores. Or Method 2: You may use the Max, Min and Sum functions.
19
Count the number of words in a textbox
string.Split() returns an array:
20
Count words example String myText = textBox1.Text;
string[] wordList = (myText.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries)); textBox2.Text = wordList.Length.ToString(); foreach (string s in wordList) { MessageBox.Show(s.ToString()); }
21
List<T> Class Represents a strongly typed list of objects that can be accessed by index. Objects stored in a list must be the same type of object. A list of type T objects A list of int, srting, etc. List resizes dynamically, no need to declare size.
22
List Property and Methods
Count Add Clear
23
Example List<int> myInts = new List<int>(); myInts.Add(5);
MessageBox.Show(myInts.Count.ToString());
24
foreach Loop List<int> myInts = new List<int>();
myInts.Add(5); myInts.Add(10); myInts.Add(11); MessageBox.Show(myInts.Count.ToString()); int sum = 0; foreach(int i in myInts) { MessageBox.Show(i.ToString()); sum += i; } MessageBox.Show(sum.ToString());
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.