1 / 33

Chapter :

Chapter :. Arrays. 1. Introduction Arrays. A group of contiguous memory locations Same name Same type Refer to particular element in the array by position number

Download Presentation

Chapter :

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 : Arrays

  2. 1. Introduction Arrays • A group of contiguous memory locations • Same name • Same type • Refer to particular element in the array by positionnumber • Refer to any element by giving the name of the array followed by the position number (subscript) of the element in square brackets ([ ]) • First element is the zeroth element • First element of array c is c[ 0 ]

  3. Arrays c[ 0 ] -45 c[ 1 ] 6 c[ 2 ] 0 c[ 3 ] 72 c[ 4 ] 1543 c[ 5 ] -89 c[ 6 ] 0 c[ 7 ] 62 c[ 8] -3 c[ 9 ] 1 c[ 10 ] 6453 c[ 11 ] -78 Name of array (Note that all elements of this array have the same name, c) Position number (index or subscript) of the element within array c Fig. 7.1 A 12-element array.

  4. Declaring and Creating Arrays • Programmer specifies the type of the elements of the array • new operator to allocate dynamically the number of elements in the array • Array declarations and initializations need not be in the same statement • In arrays of value types, each element contains one value of the declared type

  5. Creating an Array • Declare the array variable <Datatype> [ ] <NameArray>; Ex: int [ ] c; • Create the array; assign to array variable <NameArray> = new <Datatype> <[ARRAY_SIZE]>; Ex : c = new int[ 12 ]; int [ ] c = new int[ 12 ];

  6. Examples Using Arrays

  7. Examples Using Arrays

  8. Properties and methods of array • array_name.Length: return size of array • array_name.GetValue (int index): return an object at position index. • array_name[int index]: return an object at position index. • array_name.SetValue (object value, int index): add or modify an object at position index • array_name[int index] = value: add or modify an object at position index

  9. Passing Arrays to Methods • Pass arrays as arguments to methods by specifying the name of the array (no brackets) • Arrays are passed by reference • Individual array elements are passed by value

  10. Passing Arrays to Methods

  11. Passing Arrays to Methods

  12. 2. Multiple-Subscripted Arrays • Require two or more subscripts to identify a particular element • Arrays that req2uire two subscripts to identify an element are called double-subscripted arrays • Rectangular arrays • Often represent tables in which each row is the same size and each column is the same size • By convention, first subscript identifies the element’s row and the second subscript the element’s column

  13. Multiple-Subscripted Arrays Column 0 Column 1 Column 2 Column 3 Row 0 a[0, 0] a[0, 1] a[0, 2] a[0, 3] Row 1 a[1, 0] a[1, 1] a[1, 2] a[1, 3] Row 2 a[2, 0] a [2, a[2, 2] a[2, 3] 1] Column index (or subscript) Row index (or subscript) Array name

  14. Multiple-Subscripted Arrays DataType[ , ] ArrayName; ArrayName = new DataType [rowSize, colSize]; DataType [ , ] ArrayName = new DataType [rowSize, colSize];

  15. Multiple-Subscripted Arrays • GetLength (int x) • GetLength(0) • GetLength(1)

  16. Multiple-Subscripted Arrays (Jagged Arrays) DataType[ ][ ] ArrayName; ArrayName = new DataType[Size][ ]; DataType[ ][ ] ArrayName = new DataType[Size][ ];

  17. Example

  18. Example

  19. Example

  20. foreach Repetition Structure • The foreach repetition structure is used to iterate through values in data structures such as arrays • No counter • A variable is used to represent the value of each element

  21. foreach Repetition Structure

  22. ArrayList (Danh sách mảng) • Một vấn đề hạn chế của kiểu dữ liệu mảng là kích thước cố định. • Chương trình của chúng ta có thể hỏi người dùng về kích thước tuy nhiên không thực tế.

  23. ArrayList (Danh sách mảng) • Lớp ArrayList là một kiểu dữ liệu mảng mà kích thước của nó được gia tăng một cách động theo yêu cầu mà không cần cho biết trước kích thước. • Khi tạo đối tượng ArrayList, không cần thiết phải xác định số đối tượng mà nó sẽ chứa.

  24. Các thao tác với ArrayList

  25. Các thao tác với ArrayList

  26. ArrayList

  27. ArrayList

  28. HashTable • Hashtable (bảng băm) là một kiểu từ điển được tối ưu cho việc truy cập được nhanh.

  29. HashTable

  30. HashTable

  31. Example 1

  32. Example 2

  33. Example 2

More Related