Array and ArrayList

Array ISYS 350 C# 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 assigned an 0-based index. 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 a for loop

int arrayIndex; for (arrayIndex = 0; arrayIndex <= 2; ++arrayIndex) { MessageBox.Show(empName[arrayIndex].ToString()); } Using arrays Length property: number of elements in array 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 }; 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);

foreach Loop The foreach statement repeats a group of embedded statements for each element in an array. foreach loop example double[] myGPAs = new double[5] { 2.5, 3.2, 3.4, 2.9, 3.6 }; double sumGPA = 0, avgGPA; foreach (double d in myGPAs) { sumGPA += d;

} avgGPA = sumGPA / myGPAs.Length; MessageBox.Show("Average GPA is: " + avgGPA); Using Arrays 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);

Create a Loan Payment Form 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]); }

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] 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"); Sort an Array Array Class: Sort Method Sort array elements in increasing order

Example: Array.Sort(myGPAs); 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. Count the number of words in a textbox

string.Split() returns an array: https:// msdn.microsoft.com/en-us/library/tabh47cf(v=vs. 110).aspx 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()); } Using an array of seperators string[]seperators = new string[3] { "," ," ", ";" }; String myText = textBox1.Text; string[] wordList = (myText.Split(seperators, StringSplitOptions.RemoveEmptyEntries)); foreach (string s in wordList) { MessageBox.Show(s.ToString());

} JavaScript Array https://www.w3schools.com/js/js_arrays.asp Array Use [] to define array and it is not require to declare array size var names=[]; names[0]=peter; names[1]=paul; names[2]=mary;

Or simply: Names=[peter,paul,mary]; myGPAs = [ 2.5, 3.2, 3.4, 2.9, 3.6 ]; Array length, lowercase l JavaScript forEach https://www.w3schools.com/jsref/jsref_foreach.asp Array and document.write demo

Enter PV:
Select Year:
10 year
15 year
30 year

Future Value:

sort() method

By default, the sort() function sorts values as strings in ascending order. However, if numbers are sorted as strings, "25" is bigger than "100", because "2" is bigger than "1". We need to add a compare function. HTML elements innerHTML property Each HTML element has an innerHTML property that defines both the HTML code and

the text that occurs between that element's opening and closing tag. By changing an element's innerHTML after some user interaction, you can make much more interactive pages. Assigning a value: document.getElementById(p).innerHTML = 5; Example of sorting numerical array using a compare function

JavaScript Array Sort

Click the button to sort the array in ascending order.

Highest GPA is:

Lowest GPA is:

JavaScript object https://www.w3schools.com/js/js_objects.asp An object may have properties.

Example: person1 = {firstName:"John", lastName:"Doe", age:46}; person2 = {firstName:"Paul", lastName:"Smith", age:40}; Using the . notation to access property value: person1FirstName=person1.firstName; person1lastName=person1.lastName; Array of objects Note: use the array push method to add a new member

JavaScript Objects

JavaScript uses names to access object properties.

Employee Table

First Name Last Name Age

Recently Viewed Presentations

  • PowerPoint Presentation

    PowerPoint Presentation

    If the proportions of the print you want are different than your image, you should crop it. For example, if you scan a 4x6" photo, and you want to print 16x20", you have to take a little off the width....
  • Roche Template - National Institute for Biological Standards ...

    Roche Template - National Institute for Biological Standards ...

    John Saldanha, Roche Molecular Systems SoGAT XXI, 28-29 May, 2009, Brussels, Belgium. Background HBV screening of blood donations introduced in 1970s (HBsAg) In mid-1980s, anti-HBc testing introduced as surrogate marker for non-A, non-B (HCV) Risk of transfusion transmitted HBV greater...
  • Chapter 9 CORPORATE-LEVEL STRATEGY: HORIZONTAL INTEGRATION, VERTICAL INTEGRATION,

    Chapter 9 CORPORATE-LEVEL STRATEGY: HORIZONTAL INTEGRATION, VERTICAL INTEGRATION,

    Internal New Ventures. Pitfalls: Scale of Entry. Commercialization. Poor Implementation. Process of transferring/creating new business unit/division in new industry. 10-Successful Internal New Venturing. Place funding for research in hand of business unit managers.
  • Predictive factors of response to chemotherapy in colorectal ...

    Predictive factors of response to chemotherapy in colorectal ...

    The first one seems increase the MTHFR thermolability the second decreases the MTHR activity Since a loss in MTHFR enzymatic activity may favor an increase in intra-celluler 5, 10 methylenet tetra hydro folate concentration, it can be hypothesized that tumors...
  • PowerPoint Presentation

    PowerPoint Presentation

    The writing process is explained step-by-step with a full example of a sample report The chapter project might be used as the end-of-semester assignment Outline Why I chose to write this book My background General themes Selected examples Chapter projects...
  • Introduction - FEMA

    Introduction - FEMA

    In October 2017 FEMA published a revision of the National Incident Management System (NIMS). The 2017 NIMS document replaces the 2008 NIMS. This content is to provide information to emergency management practitioners on the key changes contained in the new...
  • The Costs of Production: An Introduction

    The Costs of Production: An Introduction

    Economies of scale: downward part of the LRAC curve - LRAC falls as plant size increases (costs go down, output goes up) Result of specialization and ability to purchase more efficient capital goods. Plant size doubles & total output of...
  • Understanding Art Criticism Art criticism is studying, understanding,

    Understanding Art Criticism Art criticism is studying, understanding,

    In Matisse's Harmony in Red (Red Room) 1908-9, red is the predominant color in the painting. How does the predominant redness make you feel? The color red usually makes one feel warmth because it is associated with the sun and...