1. Trang chủ
  2. » Giáo án - Bài giảng

Chapter Introduction Arrays

34 170 0
Tài liệu đã được kiểm tra trùng lặp

Đ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

Tiêu đề Arrays
Trường học Example University
Chuyên ngành Computer Science
Thể loại Lecture Note
Năm xuất bản 2023
Thành phố Sample City
Định dạng
Số trang 34
Dung lượng 282,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Chapter :

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 3

Fig 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 4

Declaring 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 5

Creating an Array

<Datatype> [ ] <NameArray>;

Ex: int[ ] c;

<Datatype> <[ARRAY_SIZE]>;

Ex : c = new int[ 12 ];

int [ ] c = new int[ 12 ];

Trang 6

Examples Using Arrays

Trang 7

Examples Using Arrays

Trang 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

Trang 9

Passing Arrays to Methods

methods by specifying the

name of the array (no

brackets)

Trang 10

Passing Arrays to Methods

Trang 11

Passing Arrays to Methods

Trang 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

Trang 13

a[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 15

Multiple-Subscripted Arrays

GetLength (int x)

– GetLength( 0 )

– GetLength( 1 )

Trang 16

Multiple-Subscripted Arrays

(Jagged Arrays)

DataType[ ][ ] ArrayName;

ArrayName = new DataType[Size][ ];

DataType[ ][ ] ArrayName = new DataType[Size][ ];

Trang 17

Example

Trang 18

Example

Trang 19

Example

Trang 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

Trang 21

foreach Repetition Structure

Trang 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

Trang 24

Cá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 25

Cá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 26

ArrayList

Trang 27

ArrayList

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 29

Values 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 30

item)

Xác định xem một thành phần có trong hashtable, trả về true hoặc false

Trang 31

Example 1

Trang 32

Example 2

Trang 33

Example 2

Trang 34

Finish

Ngày đăng: 13/05/2014, 11:39