330 likes | 442 Vues
This chapter provides a comprehensive introduction to arrays, including their definition as a group of contiguous memory locations with the same name and type. It explains how to access array elements via their position using subscripts and how to declare, create, and manipulate arrays in programming. The chapter covers value types, properties, and methods related to arrays, as well as passing arrays to methods. Additionally, it introduces multiple-subscripted arrays and the foreach repetition structure for iterating through arrays, highlighting dynamic array lists and hash tables for data structure optimization.
E N D
Chapter : Arrays
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 ]
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.
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
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 ];
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
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
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
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
Multiple-Subscripted Arrays DataType[ , ] ArrayName; ArrayName = new DataType [rowSize, colSize]; DataType [ , ] ArrayName = new DataType [rowSize, colSize];
Multiple-Subscripted Arrays • GetLength (int x) • GetLength(0) • GetLength(1)
Multiple-Subscripted Arrays (Jagged Arrays) DataType[ ][ ] ArrayName; ArrayName = new DataType[Size][ ]; DataType[ ][ ] ArrayName = new DataType[Size][ ];
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
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ế.
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.
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.