Array, ArrayList and List ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items.

Slides:



Advertisements
Similar presentations
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Advertisements

Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Arrays. Declaring a Array With subscript: –Dim numbers(2) as Integer –Using variable as subscript: Dim arrayIndex as Integer = 10 Dim myArray(arrayIndex)
Chapter 7: Working with Arrays
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Loops ISYS 350. A Box of Chocolate Repeat this process until box is empty: – Take one chocolate from the box – Eat the chocolate – Box has more chocolate?
1 nd Semester Module7 Arrays Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Rules Two Teams Questions worth 1-3 points – Entire team can confer to answer the question – Max of 2 minutes per question – You can use a computer on.
Tutorial 6 The Repetition Structure
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Enhanced For Loops (For Each Loops) Less Code and Less Overhead for Writing For Loops.
Loops ISYS 350. Compute the sum of a list of numbers: Example: 5, 2, 10, 8, 4 Process: Sum= 0 Get a number from the list Sum = Sum + the number Repeat.
Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Shipping Hub Application.
Research Topics in Computational Science. Agenda Survey Overview.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Computers, Variables and Types Engineering 1D04, Teaching Session 2.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.
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.
Coding ADO.NET Objects: Connection, Command, DataReader.
Loops ISYS 350. Two Types of Loops while loop for loop.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Visual Basic Fundamental Concepts
Array ISYS 350.
Loops ISYS 350.
Chapter 7: Working with Arrays
Tutorial 9 - Car Payment Calculator Application Introducing the while Repetition Statement Outline 9.1 Test-Driving the Car Payment Calculator Application.
Loops ISYS 350.
Programming Interface Controls
Programming Interface Controls
Array ISYS 350.
Array ISYS 350.
Visual Basic..
Array ISYS 350.
Loops ISYS 350.
Array ISYS 350.
Java Array ISYS 350.
Chapter (3) - Looping Questions.
CIS 16 Application Development Programming with Visual Basic
Loops ISYS 350.
Lecture Set 10 Windows Controls and Forms
Module8 Multi-dimensional Array
Loops ISYS 350.
Loops ISYS 350.
Array ISYS 350.
Array ISYS 350.
Loops ISYS 350.
Programming Interface Controls
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Creating Controls Dynamically in C#
Presentation transcript:

Array, ArrayList and List 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 intRate[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 a for loop Using array’s Length property: int arrayIndex; 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 }; 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);

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);

Create a Loan Payment Form

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; 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 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"));

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.

ArrayList Property and Methods ArrayList – Count – Add – Clear

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

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

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

List 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 List resizes dynamically as arraylist.

List Property and Methods List – Count – Add – Clear

Example Adding three invisible textboxes to a form with background color of Green, Yellow and Red. Declaring a List containing the 3 textboxes. Use a timer’s click event to show the textboxes in rotation.

List myBox = new List (); private void Form14_Load(object sender, EventArgs e) { myBox.Add(textBox1); myBox.Add(textBox2); myBox.Add(textBox3); } int i=0,j; private void timer1_Tick(object sender, EventArgs e) { j = i % 3; for (int k = 0; k < myBox.Count;k++ ) { if (k == j) myBox[k].Visible = true; else myBox[k].Visible = false; } i++; }