Visual Basic 2010 How to Program © 1992-2011 by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Chapter 10 Introduction to Arrays
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Chapter 71 Array Properties Upper Bound: The value of arrayName.GetUpperBound(0) is the upper bound of arrayName(). Then What is the Lower Bound ? Ans:
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9: Arrays and Strings
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
JavaScript, Third Edition
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
Created By Mayson Al-Duwais1. Using Exit to Terminate Repetition Statements To terminate different types of repetition statements you can use a special.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
 Pearson Education, Inc. All rights reserved Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 8: Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 7 – Arrays 7.1 Creating and Using Arrays 7.4 Two-Dimensional Arrays.
Chapter 7 – Arrays 7.1 Creating and Accessing Arrays
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
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.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Arrays 1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Arrays Chapter 7.
Chapter 6 JavaScript: Introduction to Scripting
© 2016 Pearson Education, Ltd. All rights reserved.
Visual Basic 2010 How to Program
JavaScript: Functions.
Chapter 8 JavaScript: Control Statements, Part 2
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Visual Basic 2010 How to Program
Chapter (3) - Looping Questions.
Arrays.
CIS16 Application Development and Programming using Visual Basic.net
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1

 An array is a group of variables (called elements) containing values that all have the same type.  To refer to a particular element in an array, we specify the name of the array and the position number of the element to which we refer. © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 2

Dim arrayName(n) As DataType  0 is the lower bound of the array  n is the upper bound of the array–the last available subscript in this array  The number of elements, n + 1, is the size of the array.  You can determine the size of the array using the system method (length) arrayName.Length © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 3

4

Dim C(11) As Integer  0 is the lower bound of the array  C(0) value equals to -45  11 is the upper bound of the array  C(11) value equals to 78  The number of elements, 12, is the size of the array  C.Length = 12 © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 5

© by Pearson Education, Inc. All Rights Reserved. -Maysoon Al-Duwais 6 arrayName.Countnumber of elements arrayName.Lengthnumber of elements arrayName.Maxhighest value arrayName.Minlowest value arrayName.Firstfirst element arrayName.Lastlast element arrayName.GetUpperBound(0)The upper bound value arrayName.GetLowerBound(0)The lower bound value numArrayName.Averageaverage value of elements numArrayName.Sumsum of values of elements

Dim array1() As Integer = {6,2,8} © by Pearson Education, Inc. All Rights Reserved. -Maysoon Al-Duwais 7 array1.Count3 array1.Length3 array1.Max8 array1.Min2 array1.First6 array1.Last8 array1.GetUpperBound(0)2 array1.GetLowerBound(0)0 array1.Average5.3 array1.Sum16

◦ The position number in parentheses is called an index it can be:  Nonnegative integer. Example: C(3)  Or integer expression. Example: if value1 = 5, value2 = 6 c(value1 + value2) += 2 c(5 + 6) += 2 c(11) += 2 C(11) = C(11) + 2 C(11) = C(11) =80 © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 78 C(11) 80 C(11) + 2 8

 Values stored in arrays can be used in calculations.  For example, 1)sum = c(0) + c(1) + c(2) sum = sum = -39 2)result = c(6) \ 2 result = 0 \ 2 result = 0 © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 9

Different ways to declare array: 1.Dim c(0 To 3) As Integer 2.Dim c(3) As Integer 3.Dim c() As Integer = { 9, 2, 6, 1 } 4.Dim c() = { 1, 2, 3, 6 } ◦ The lower bound of all the three arrays above is 0 and the upper bound is 3. ◦ The size of all the three arrays above equals to 4. ◦ In the last two array declarations, we declared & initialize the array without specifying the upper bound value. © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 10

1.Dim c() As Integer = { 9, 2, 6, 1 } 2.Dim c() = { 1, 2, 3, 6 }  When the initializer list is used, you cannot specify the upper bound value.  So, if you write the above declaration as follows: Dim c(3) As Integer = {9, 2, 6, 1} You will get a Syntax Error © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais Initializer List X 11

 When you do not provide an initializer list, the elements in the array are initialized to the default value for the array’s type as follows: ◦ 0 for numeric primitive data-type variables ◦ False for Boolean variables ◦ Nothing for String and other class types. © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 12

© by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 13

© by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 14

 Figure 7.2 creates two five-element integer arrays and sets their element values, using an initializer list and a For … Next statement that calculates the element values, respectively.  Line 13 declares and allocates array2, whose size is determined by the expression array1.GetUpperBound(0) = 4 array1.GetLowerBound(0) = 0 Array1.Length = 5 © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 15

Dim array2(array1.GetUpperBound(0)) As Integer Dim array2(4) As Integer This means that array2 will have the same size of array 1: array2.GetUpperBound(0) = 4 array2.GetLowerBound(0) = 0 Array2.Length = 5 © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 16

 In Example 6 the greatest value in a numeric array ages is determined.  The value of the variable max is set to the first element of the array.  Then a For…Next loop successively examines each element of the array and resets the value of max when appropriate. 17 © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Dim ages() As Integer = {55, 56, 61, 52, 69, 64, 46, 54, 47} 'last 9 presidents Dim max As Integer = ages(0) For i As Integer = 1 To ages.Count - 1 If ages(i) > max Then max = ages(i) End If Next txtOutput.Text = "Greatest age: " & max Output: Greatest age: © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

19 maxages(i)i 55ages(1) = ages(2) = ages(3) = ages(4) = ages(5) = ages(6) = ages(7) = ages(8) = 47 8 __ 9

 Have type Boolean  Used when looping through an array  Provide information to be used after loop terminates. Or, allows for the early termination of the loop. 20 © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

1. Dim Names() As String = {"hend", "manal", "asma", "sarah“, “nouf”, “Lamya”} 2. Dim nameFound As Boolean = False ‘ The Flag Variable 3. Dim Name_Start_with_A As String = Nothing 4. Dim upperName As String = Nothing 5. Dim i As Integer = Do While ( Not nameFound ) 8. upperName = Names(i).ToUpper If upperName.StartsWith("A") Then 'Search a name that starts with ‘A’ 11. nameFound = True 12. Name_Start_with_A = Names(i) 13. End If 14. i += Loop Label1.Text = "A Name that starts with A = " & Name_Start_with_A © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 21

© by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 22

Name_Start_with_ANot namesFoundnameFoundupperNameNames(i)i NothingTrueFalseHENDhend0 NothingTrueFalseMANALmanal1 asmaFalseTrueASMAasma2 ____3 © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 23 Loop will stop here (when i =3) because the Do While....Loop condition is not met (When the flag variable nameFound = True Not nameFound = False )

For i As Integer = 1 To ages.Count - 1 If ages(i) > max Then max = ages(i) End If Next can be replaced with For Each age As Integer In ages If age > max Then max = age End If Next 24 © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

 In the For…Next loop, the counter variable i can have any name.  In the For Each loop, the looping variable age can have any name.  The primary difference between the two types of loops is that in a For Each loop no changes can be made in the values of elements of the array. 25 © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

A statement of the form numVar = Array.IndexOf(arrayName, value) assigns to numVar the index of the first occurrence of value in arrayName. Or assigns -1 if the value is not found. 26 © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Dim numbers() As Integer = {8, 2, 6, 6, 6} Label1.Text = "Array.IndexOf(numbers, 6)=" & Array.IndexOf(numbers, 6) & vbCrLf Label1.Text &= "Array.LastIndexOf(numbers, 6)=" & Array.LastIndexOf(numbers, 6) © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 27

28 If arrayOne and arrayTwo have been declared with the same data type, then the statement arrayOne = arrayTwo makes arrayOne an exact duplicate of arrayTwo. Actually, they share the same location in memory. © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Dim Names() As String = {"hend", "asma", "manal", "sarah"} Dim Names2(1) As String Names2 = Names For Each element In Names2 Label1.Text &= “element = " & element & vbCrLf Next © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 29

30  Split can convert a string containing comma-separated data into a string array. ArrayName = StringName.Split(“SplitCharacter”) Split Character also called delimiter could be:  Comma “,”  Dot “.”  Start “*”  Semicolon “;”  Or any other character  If no character is specified, the space character “ “ will be used as the delimiter. © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

31 Dim employee() As String Dim line As String = "Bob;23,50;45" employee = line.Split(“;") For i = 0 To employee.GetUpperBound(0) Label1.Text &= "employee(" & i & ") = " & employee(i) & vbCrLf Next  sets the size of employees to 3  sets employees(0) = “Bob”  sets employees(1) = “23,50”  sets employees(2) = “45” © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

32  The reverse of the Split method is the Join function.  Join concatenates the elements of a string array into a string containing the elements separated by a specified delimiter. © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais

Dim greatLakes() As String = {"Huron","Ontario", "Michigan","Erie","Superior"} Dim lakes As String lakes = Join(greatLakes, ",") txtOutput.Text = lakes Output: Huron,Ontario,Michigan,Erie,Superior © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 33

34 The following code references an array element that doesn't exist. This will cause an error. © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais