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... ArrayList Danh sách mảng• L
Trang 1Chapter :
Arrays
Trang 2• Refer to any element by giving the name of the array followed by the position number ( subscript ) of the element in square brackets ([ ])
element
Trang 3Fig 7.1 A 12-element array
-45 6 0 72 1543 -89 0 62 -3 1 6453 -78
Position number (index or
subscript) of the element
within array c
Name of array (Note that all
elements of this array have
the same name, c)
c[ 11 ] c[ 10 ] c[ 9 ] c[ 8]
c[ 7 ]
c[ 4 ] c[ 3 ] c[ 2 ] c[ 1 ] c[ 0 ]
c[ 6 ] c[ 5 ]
Trang 4Declaring and Creating Arrays
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
Trang 5Creating an Array
<Datatype> [ ] <NameArray>;
Ex: int[ ] c;
<Datatype> <[ARRAY_SIZE]>;
Ex : c = new int[ 12 ];
int [ ] c = new int[ 12 ];
Trang 6Examples Using Arrays
Trang 7Examples Using Arrays
Trang 8Properties 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
Trang 9Passing Arrays to Methods
methods by specifying the
name of the array (no
brackets)
Trang 10Passing Arrays to Methods
Trang 11Passing Arrays to Methods
Trang 122 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
Trang 13a[0, 0] a[0, 1] a[0, 2] a[0, 3]
a[1, 0] a[1, 1] a[1, 2] a[1, 3]
a[2, 0] a[2, 2] a[2, 3]
Column index (or subscript) Row index (or subscript)
a [2, 1]
Trang 15Multiple-Subscripted Arrays
• GetLength (int x)
– GetLength( 0 )
– GetLength( 1 )
Trang 16Multiple-Subscripted Arrays
(Jagged Arrays)
DataType[ ][ ] ArrayName;
ArrayName = new DataType[Size][ ];
DataType[ ][ ] ArrayName = new DataType[Size][ ];
Trang 17Example
Trang 18Example
Trang 19Example
Trang 20foreach 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
Trang 21foreach Repetition Structure
Trang 23ArrayList (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
Trang 24Các thao tác với ArrayList
Add ( object value) Phương thức public để thêm một đối tượng vào
ArrayList
Insert ( int index,
object value)
Chèn một thành phần vào trong ArrayList
Remove ( object obj) Xóa sự xuất hiện đầu tiên của một đối tượng
xác định
RemoveAt ( int index) Xóa một thành phần ở vị trí index.
Count Thuộc tính nhận số phần tử hiện thời trong
mảng
Clear() Xóa tất cả các thành phần từ ArrayList
Contains ( object item) Kiểm tra một thành phần xem có chứa trong
mảng hay không, trả về true hoặc false
Trang 25Các thao tác với ArrayList
BinarySearch() Tìm xem một phần tử có trong ArrayList
không, sử dụng phương pháp tìm kiếm nhị phân
Clone() Tạo một bản copy
CopyTo() Sao chép ArrayList đến mảng một chiều
RemoveRange() Xóa một dãy các thành phần
Reverse() Đảo thứ tự các thành phần trong ArrayList
SetRange() Sao chép các thành phần của tập hợp qua dãy
những thành phần trong ArrayList
Sort() Sắp xếp ArrayList
Trang 26ArrayList
Trang 27ArrayList
Trang 28• 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 Key Value
Trang 29Values Thuộc tính trả về một ICollection chứa các
giá trị
khóa trong hashtable.
Remove (object obj) Xóa một thành phần với khóa xác định.
Count Thuộc tính trả về số thành phần trong
hashtable
Clear() Xóa tất cả đối tượng trong hashtable.
Trang 30item)
Xác định xem một thành phần có trong hashtable, trả về true hoặc false
Trang 31Example 1
Trang 32Example 2
Trang 33Example 2
Trang 34Finish