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.

Slides:



Advertisements
Similar presentations
Review Generics and the ArrayList Class
Advertisements

VB List(Of type) A List can be thought of as an array that automatically adjusts its size as elements are added and removed A List may hold only objects.
Two Dimensional Arrays and ArrayList
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Programming with Collections Collections in Java Using Arrays Week 9.
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.
© The McGraw-Hill Companies, 2006 Chapter 5 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
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
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.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
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 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects.
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.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Arrays and Collections Tonga Institute of Higher Education.
© 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
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
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.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
Loops ISYS 350. Two Types of Loops while loop for loop.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array.
Visual Basic.NET BASICS Lesson 11 List Boxes, For Next Loops, and Label Settings.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
Loop Continue ISYS 350. Exiting a Loop Prematurely In some cases it is necessary to end a loop before the test condition would end it Use the break statement:
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Introduction to programming in java Lecture 21 Arrays – Part 1.
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.
Passing Objects to Methods
Array ISYS 350.
Loops ISYS 350.
Chapter 7: Working with Arrays
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Loops ISYS 350.
Repeating Program Instructions
Array ISYS 350.
Array ISYS 350.
Array ISYS 350.
Loops ISYS 350.
Array ISYS 350.
Java Array ISYS 350.
Object Oriented Programming in java
Loops ISYS 350.
Module8 Multi-dimensional Array
Managing Collections of Data
Loops ISYS 350.
Array ISYS 350.
Array ISYS 350.
Loops ISYS 350.
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Presentation transcript:

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

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

Binding an arrayList to a control: Control’s DataSource property A string arrayList to store rates with “%”: – ArrayList strRateList = 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: – ArrayList rateList = new ArrayList() {.03,.04,.05,.06,.07,.08 }; Use listbox selectedIndex to access the rate: rate = (double)rateList[listBox1.SelectedIndex]; Note: Need the (double) cast operator.

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