Publications: 83 | Followers: 0

Array and ArrayList - San Francisco State University

Publish on Category: Birds 0

Array
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
Declare an array in one statement:Type[]arrayName= new type[array size];Ex:string[]empName= new string[3];double[]intRate= new double[6];
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]
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";
Accessing Array Elements with afor loop
Using array’s Length property:
intarrayIndex;for (arrayIndex= 0;arrayIndex<= 2; ++arrayIndex){MessageBox.Show(empName[arrayIndex].ToString());}
for (arrayIndex= 0;arrayIndex<= empName.Length-1; ++arrayIndex){MessageBox.Show(empName[arrayIndex].ToString());}
Note: Length - 1
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 };doublesumGPA=0,avgGPA;for (inti= 0;i<=myGPAs.Length- 1; ++i){sumGPA+=myGPAs[i];}avgGPA=sumGPA/myGPAs.Length;MessageBox.Show("Average GPA is: " +avgGPA);
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 };doubleavgGPA,maxGPA,minGPA;avgGPA=myGPAs.Average();maxGPA=myGPAs.Max();minGPA=myGPAs.Min();MessageBox.Show(“The size of array is: " +myGPAs.Length);
Create a Loan Payment Form
If rates are stored in an array
Method 1:Using a loop to createtheListboxwith ratesinthe Form Load event
string[]strRate= new string[6] { "3%", "4%", "5%", "6%", "7%", "8%" };for(inti=0;i<=strRate.Length-1;i++){listBox1.Items.Add(strRate[i]);}
Method 2: Data BindingBindingan array to a control’sDataSourceproperty
A string array to store rates with “%”:string[]strRate= new string[6] { "3%", "4%", "5%", "6%", "7%", "8%" };Bind the array to alistbox:listBox1.DataSource =strRate;Using the Form Load event
Data Binding Example
private void Form1_Load(object sender,EventArgse){string[]strRate= new string[6] { "3%", "4%", "5%", "6%", "7%", "8%" };listBox1.DataSource =strRate;}
Parallel Array Example
A parallel array to store the numerical rates:double[]intRate= new double[6] { .03, .04, .05, .06, .07, .08 };UselistboxselectedIndexto access the rate:intRate[listBox1.SelectedIndex]
Code Example
double[]intRate= new double[6] { .03, .04, .05, .06, .07, .08 };doubleloan, rate, term, payment;loan=double.Parse(textBox1.Text);rate=intRate[listBox1.SelectedIndex];if(radioButton1.Checked)term = 15;elseterm = 30;payment=Financial.Pmt(rate / 12, term * 12, -loan);textBox2.Text=payment.ToString("c");
Sort an Array
Array Class: Sort MethodSort array elements in increasing orderExample:Array.Sort(myGPAs);
WeightedAvgof three exams=60%*highest score +30%*2ndhighest score +10%*lowest score
Method 1: You may sort the array of exam scores.OrMethod 2: You may use the Max, Min and Sum functions.

0

Embed

Share

Upload

Make amazing presentation for free
Array and ArrayList - San Francisco State University