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.

Slides:



Advertisements
Similar presentations
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
Advertisements

Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
 “Regular” variable ◦ Holds a single value ◦ For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Studentn as String.
Programming with Collections Collections in Java Using Arrays Week 9.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 8 Arrays and Collections Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
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.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Programming In Visual Basic.NET
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 8 Arrays and Strings
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
User-defined Data Types VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables.
 Pearson Education, Inc. All rights reserved Arrays.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Lists in Python.
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 8 Arrays and Strings
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Data Strcutures.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
8-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Arrays Chapter 8.
Arrays. Overview u General Discussion  Uses  Structure  Declaration u Searching u Control Arrays.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 8 Arrays.
Arrays and Collections Tonga Institute of Higher Education.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
Chapter 8 Arrays Programming In Visual Basic.NET.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Chapter 8 P 1 Arrays and Grids Single-dimension arrays Definition An array is a sequence of elements all referred to with a common name. Other terms: table,
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 8 Arrays Brief description on how to navigate within this presentation (PPT) The first time a Key Term from the chapter is used in the PPT, it.
2. Understanding VB Variables
Visual Basic .NET BASICS
Brief description on how to navigate within this presentation (ppt)
Chapter : Arrays.
Arrays.
Programming In Visual Basic .NET
Lecture Set 9 Arrays, Collections, and Repetition
Presentation transcript:

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 A subscript or index is a zero-based integer used to reference the specific elements in the array (0) (1) (2) (3) (4) (5) (6) (7) (8) (9) Janet Baker George Lee Sue Li Samuel Hoosier Sandra Weeks William Macy Andy Harrison Ken Ford Denny Franks Shawn James studentName

Array Subscripts Subscripts may be numeric constants, numeric variables, or numeric expressions. Subscripts must be integers — VB rounds any non-integer subscript. Arrays are bounded by the lower subscript, which is always 0, and the upper subscript. VB throws exceptions for subscripts that are out of range. The size of the array is one greater than the upper bound.

The Declaration Statements for Arrays Arrays can be declared using Dim, Public, Private or Friend. Numeric variables are initialized to 0 and string elements to the empty string. Three forms are available: Important Note: You can declare an array with an upper bound or an initial value list, but not both. Dim ArrayName(UpperSubscript) As Datatype Dim ArrayName( ) As Datatype = {InitialValueList} Dim ArrayName As Datatype( ) = {InitialValueList}

Array Declaration Examples Dim student(2) As String Dim student() As String = {“John”, “Mary”, “Tom”} Dim student As String() = {“John”, “Mary”, “Tom”} These 3 statements CORRECTLY declare an array of 3 elements. This statement INCORRECTLY declares an array of 3 elements. Dim student(2) As String = {“John”, “Mary”, “Tom”}

For Each/Next Statement Use loops to reference each element in the array. For Each/ Next makes one pass through the loop per element The looping variable ElementName must be same type as the array. For Each ElementName [As Datatype] In ArrayName ' Statement(s) in loop. Next [ElementName] Dim students() As String = {“John”, “Mary”, “Tom”} For Each OneName As String In students Debug.WriteLine(OneName) Next OneName

Debugging Arrays View the array elements in debugging time by setting a breakpoint and view the Autos window; click the plus sign to left of array name to view individual array elements.

Direct Reference Versus Table Look Up Sometimes, if the information is sequential, you can use the information provided by the user as an index into an array. This is known as a direct reference. Dim monthTable() As Integer = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} Dim November, daysInNovember As Integer November = 11 daysInNovember = monthTable(November-1) direct reference into monthTable

Direct Reference Versus Table Look Up (2) Other times, if the information is not sequential, you must use the information provided by the user indirectly through a table look up to get an index into an array. Dim socialSecurityNumber() As Integer = { , , , } Dim employeeName() As String = {“A. Smith”, “B. Jones”, “C. Davis”, “D. Johnson”} For index As Integer = 0 to 3 If socialSecurityNumber(index) = Integer.Parse(ssTextbox.Text) Then Debug.WriteLine(employeeName(index)) Exit For End If Next index

Using List Boxes With Arrays Index = GroupListBox.SelectedIndex Allow the user to select from a list and the SelectedIndex property can be used as the subscript of the Sales array. Table look up is easy when a list box is used on the form.

Multidimensional Arrays To declare a two-dimensional array or table : Dim ArrayName(HighestRowSubscript, HighestColumnSubscript) As Datatype or Dim ArrayName(, ) As Datatype = {ListOfValues} Dim StudentNames(2,3) As String or Dim StudentNames(, ) As String = { {"James", "Mary", "Sammie", "Sean"}, {"Tom", "Lee", "Leon", "Larry"}, {"Maria", “Jane", "Jill", "John"} }

The Previous Example Sets Up This 2D Array (0, 0) James (0, 1) Mary (0, 2) Sammie (0, 3) Sean (1, 0) Tom (1, 1) Lee (1, 2) Leon (1, 3) Larry (2, 0) Maria (2, 1) Jane (2, 2) Jill (2, 3) John Column 0 Column 1 Column 2 Column 3 Row 0 Row 1 Row 2

Example: Summing a Two-Dimensional Table

Code for Summing 2D Array Dim RowSum() As Decimal = {0D, 0D, 0D, 0D} Dim ColSum(5) As Decimal For Row As Integer = 0 To 3 For Col As Integer = 0 To 5 RowSum(Row) += amount(Row, Col) Next Col Next Row For Col As Integer = 0 To 5 For Row As Integer = 0 To 3 ColSum(Col) += amount(Row, Col) Next Row Next Col

.NET Collections Collections are more fully powered arrays used to handle a group of related data. Collections aren’t fixed length, unlike arrays. Collections provide special object-oriented features (methods) for doing things like: adding/ deleting items using keys, not just indices, for accessing items sorting/ clearing the collection, etc. Example collections available in System.Collections ArrayList SortedList Queue Stack

SortedList Collection Elements automatically sorted. Items are key – value pairs Elements can be referenced by key or index. Features many methods to aid in data handling.