1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Learning programming using Visual Basic Net – Chapter 10 Working with arrays and collections

29 339 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 29
Dung lượng 371,5 KB

Nội dung

After studying this chapter you should be able to Explain why arrays are needed to solve many types of problems; construct an array to store multiple related data values; use ArrayList, Hashtable, and SortedList collections to store and process data values.

CHAPTER TEN Working with Arrays and Collections 10- Introduction • An array is a variable with a single symbolic name that represents many different data items • Visual Basic NET provides a number of classes that manage collections of objects • A collection of objects is like an array in that one collection is associated with many objects • This chapter covers the ArrayList, Hashtable, and SortedList collections McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 10- Objectives • Explain why arrays are needed to solve many types of problems • Construct an array to store multiple related data values • Use ArrayList, Hashtable, and SortedList collections to store and process data values McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 10- 10.1 Solving Problems with Arrays • Simple variable means a variable that can store only one value • The Problem – Calculate the rate of return for 10 different companies and those companies who exceeded the average of the group McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 10- 10.1 Solving Problems with Arrays (cont.) • The Solution Using Simple Variables – Write the pseudocode for the problem solution • Two passes through the data: – One to compute the average – Two to determine who exceeds the average – Not a good way to solve the problem McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 10- 10.1 Solving Problems with Arrays (cont.) • The Structure of an Array – – – – – Each element of an array is like a simple variable Starting at zero, the elements in an array are numbered The element number is called the subscript Another name for the element number is index value Arrays are also called subscripted or indexed variables McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 10- 10.1 Solving Problems with Arrays (cont.) • In a one-dimensional array, each element is referenced using a single subscript value • Higher-dimensional arrays are called multidimensional arrays • Syntax for a one-dimensional array: – ArrayName(SubscriptValue) McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 10- 10.1 Solving Problems with Arrays (cont.) • The Solution Using Arrays – An array will provide a better problem solution – Use a numeric variable for a subscript – The subscript begins at zero McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 10- 10.1 Solving Problems with Arrays (cont.) • Storing Data in Arrays versus Databases – – – – – Reading data from a database is an option Arrays are variables that are stored in RAM Data stored in RAM can be accessed quickly Storage for arrays in RAM is valuable space Database are stored on disk and retrieval is relatively slow McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 10.1 Solving Problems with Arrays (cont.) • Factors to consider for array versus database are: Execution speed Volume of data Clarity of the code that accomplishes the task • As RAM gets less expensive, the distinction between databases and arrays gets less clear McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1010 10.3 The ArrayList Collection (cont.) – Properties • Capacity • Count • Item McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1015 10.3 The ArrayList Collection (cont.) – Methods Add Remove BinarySearchRemoveAt Clear Reverse Contains Sort IndexOf ToArray Insert TrimToSize McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1016 10.3 The ArrayList Collection (cont.) • Complete the examples listed below in your textbook: – Example 10.3 Populating an ArrayList from a Database – Example 10.4 Performing a Binary Search – Example 10.5 Performing Multiple Result Search McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1017 10.4 The Hashtable Collection • A Hashtable is a data structure that supports very fast access to information based on a key field • The hash function takes a unique key field and transforms it into a new value called a bucket number McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1018 10.4 The Hashtable Collection (cont.) – Important Points about Hashtables: • Support very fast access • Store the records randomly • Sequential access to the entire set of records is difficult • Exact key must be used for access McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1019 10.4 The Hashtable Collection (cont.) – Properties • • • • Count Item Keys Values McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1020 10.4 The Hashtable Collection (cont.) – Methods • • • • • • • Add Clear Contains ContainsKey ContainsValue GetEnumerator Remove McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1021 10.4 The Hashtable Collection (cont.) • Complete the examples listed below in your textbook: – Example 10.6 Populating a Hashtable from a Database Table – Example 10.7 Searching a Hashtable McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1022 10.5 The SortedList Collection • A SortList is a hybrid between a Hashtable and an ArrayList • A SortedList maintains an array for the keys and another array for the associated values • The elements of a SortedList are ordered by the value of the keys • Operations on a SortedList tend to be slower than operations on a Hashtable McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1023 10.5 The SortedList Collection (cont.) – Properties • • • • Count Item Keys Values McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1024 10.5 The SortedList Collection (cont.) – Methods Add Clear ContainsKey ContainsValue GetByIndex GetEnumerator GetKey McGraw Hill/Irwin IndexOfKey IndexOfValue Remove RemoveAt SetByIndex TrimToSize ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1025 10.5 The SortedList Collection (cont.) • Complete the examples listed below in your textbook: – Example 10.8 Populating a SortedList from a Database Table – Example 10.9 Finding Average Rate of Return McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1026 Chapter Summary • An array is a variable that stores more than a single value • Arrays must be declared using a Dim, Static, or Public statement • Arrays also need to be dimensioned • To access an element of an array, the programmer specifies both the array name and specific subscript values McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1027 Chapter Summary (cont.) • The declaration statement that creates an array initializes all of its elements to zero or the zerolength string • We start by populating the array • Then, we can use the array to process data • An ArrayList collection is an index-based data structure like an array • An ArrayList does not have a set capacity McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1028 Chapter Summary (cont.) • A Hashtable collection is a key-based data structure • Unlike an array, a Hashtable collection uses a key field to store and retrieve elements • Hashing has a randomizing effect • A SortedList provides both index-based and keybased access to its elements • A SortedList shares the capabilities of the ArrayList and Hashtable McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 1029 ... Inc All rights reserved 10- 10. 1 Solving Problems with Arrays (cont.) • Storing Data in Arrays versus Databases – – – – – Reading data from a database is an option Arrays are variables that... rights reserved 10- 10. 1 Solving Problems with Arrays (cont.) • The Solution Using Arrays – An array will provide a better problem solution – Use a numeric variable for a subscript – The subscript... databases and arrays gets less clear McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 101 0 10. 1 Solving Problems with Arrays (cont.) • Multidimensional Arrays – The number

Ngày đăng: 16/05/2017, 14:42

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN