1 / 18

John L. Cleveland, Instructor

Chapter 8: Arrays. John L. Cleveland, Instructor. Arrays 1. Group of Variables (called elements ) that have the same type. To refer to an array, we refer to the array name and position of the element. Every array begins at the zero element.

love
Download Presentation

John L. Cleveland, Instructor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8: Arrays John L. Cleveland, Instructor

  2. Arrays 1 • Group of Variables (called elements) that have the same type. • To refer to an array, we refer to the array name and position of the element. • Every array begins at the zero element. • Position number in parenthesis is called an index or subscript. • Array number much be a non-negative integer, • An indexed array name is an lvalue, and can be used on left side of an assignment statement to place a new value into an array.

  3. Arrays 2 • In VB, every array “knows” its own length. • Including the Length property, all arrays have access to methods and properties of the System.Array class. • The last element in an array can be fetched with GetUpperBounds.

  4. Declaring and Allocating Arrays • To declare and array, you provide array’s name and type, Dim C as Integer(). • The declaration creates the a variable but stores no value in memory. • Arrays can be declared to contain any type. • Before using an array, specify size and allocate memory. • The initializer list in brackets {} specifies initial values which can be a comma-separated list. • When the initializer list is empty, the elements are initialized to their default values.

  5. Using Arrays • All elements in an array must be of the same type. • VB provides mechanisms that prevent accessing elements outside the bounds of the array.

  6. Passing an Array to a Method • Pass an array name to a methods with no parenthesis. • Every array element knows its own upper bound so no need to pass the upper bound as a separate argument. • The method’s parameter list must specify that an array will be received • It is normally inappropriate to specify that an array be passed using the keyword ByRef since the are passed by reference by default in VB. • Used the indexed name of the array element as an argument in the method call to pass an array element

  7. For Each…Next • Iterate through an array using the For Each…Next repetition statement. • When used with a one-dimensional array, for each…next iterates through the range of values from the zero element through to the value returned by GetUpperBound().

  8. Sorting an Array • Sort data in ascending or descending order is one of the most popular computing applications. • Class Array provides methods for creating, modifying, sorting and searching arrays. • Shared method Sort of Class Array takes an array as its argument and sorts in ascending order. • To sort in descending order, first call method Sort and then method Reverse.

  9. Searching an Array • Process of locating a particular key element value in an array is called searching. • Linear method works well for small or unsorted arrays, but is inefficient for large unsorted arrays. If the array is unsorted, high-speed binary search can be used. • The Array class provides a method BinarySearch which is used to search a sorted array.

  10. Rectangular and Jagged Arrays • One-dimensional arrays contain one row of values. • Multi-dimensional require two or more indices to identify particular elements. • Two types of two-dimensional arrays—rectangular and jagged. • In rectangular arrays, each row is the same size. • Jagged arrays are maintained as arrays of arrays and rows can be of different sizes. • In jagged arrays, the second dimension actually is an index into the one-dimensional index representing the current row. • Use next For…Next to traverse each dimension of multidimensional array.

  11. Variable-length Parameter lists • ParamArray used by methods a variable number of arguments. • All arguments passed to the ParamArray must be of the same type. • Otherwise a compilation error occurs when Option Strict is on.

  12. ReDim • Use to change array size dynamically at program execution time. • Can change the array size but not the number of elements or type. • ReDim ArrayName(New UpperBound) • The keyword Preserve after ReDim saves the old values and upper bound but if the new array is smaller than old, some values may be lost.

  13. ByVal versus ByRef 1 • Reference types like arrays and other objects are actually passed by reference even if the ByVal keyword is used. • That means changes made to the objects in called methods is actually made to the original objects in the callers. • ByRef is another way arguments can be passed. • When ByRef is used, the caller actually gains control over the original reference in the caller. • That means the calling method can actually replace the the caller with a reference to a different object or even Nothing • Such behavior can lead to disastrous results. You always need to keep in mind what values you are changing.

  14. ByVal versus ByRef 2 • IsNot operator can compare two referenced objects to see if they are the same. If not, IsNot returns False.

  15. Other Terms to Know 1 • Allocate an array with New • Array as an Object • Array Declaration • Array Initialized to zeroes • Array of arrays • Bar Chart • Double subscripted arrays • Exception for invalid array indexing • Explicit Array bounds

  16. Other Terms to Know 2 • Ignoring array Element zero • IndexOutofRange Exception • Inner For Statements • Inner Loop • Innermost set of parenthesis • Iterative Binary search • Jagged Array • Key Value in Searching

  17. Other terms to Know 3 • M-by-n array • Off-by-one array • Outer For Statement • Subarray • Table • Table Elements • “Walk” past the end of an array • Zero-based Counting

  18. Assignment Week 2Arrays in Chapter 8. • Pages 394-395. Self-review exercises 8-1 through 8.2 • Pages 395- 397. Exercises 8.3 through 8.9

More Related