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

Chapter 5 Arrays - Collections

31 376 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 - Collections
Trường học University of Example
Chuyên ngành Computer Science
Thể loại Textbook
Năm xuất bản 2010
Thành phố Sample City
Định dạng
Số trang 31
Dung lượng 836,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

Properties - Methods of an array returns size of array  array_name.. GetValue int index  array_name[ int index]  returns an object at position index  array_name.. SetValue object

Trang 1

Arrays - Collections

Chapter 5

Ebook: Beginning Visual C# 2010, part 1, chapter 5, 11

Trang 2

Arrays

Collection classes

Trang 4

Declaring and Allocating arrays

int[] a;

a = new int[12];

string[] b = new string[100];

double[] array1 = new double[10], array2 = new double[20];

When arrays are allocated, the elements are initialized:

 zero for the numeric data-type

 false for bool variables

 null for reference types

ArrayName = new DataType [size]; // allocate array

// declare and allocate array

DataType [ ] ArrayName = new DataType [size];

Trang 5

Initializing an array

Arrays can be declared, allocated, initialized in a

statement

int [] myIntArray = new int [5] { 2, 4, 6, 8, 10};

Allocate space for the array – number of elements

in initializer list determines the size of array

int [] myIntArray = { 2, 4, 6, 8, 10};

Trang 6

foreach Loops

Example:

foreach ( <baseType> <var_name> in

<array> ) {

// can use <var_name> for each element }

Trang 7

Properties - Methods of an array

 returns size of array

array_name GetValue ( int index)

array_name[ int index]

 returns an object at position index

array_name SetValue ( object value, int index)

array_name[ int index] = value

 modifies an object at position index

Trang 8

string output = "" ;

const int ARRAY_SIZE = 10 ;

int [] y = { 32 , 27 , 64 , 18 , 95 , 14 , 90 , 70 , 60 , 37 };

int [] z;

z = new int [ ARRAY_SIZE ];

"\t" + z.GetValue(i) + "\n" ;

Trang 9

Array parameter

No need the number of elements in the parameter array when you define the method

 Example:

 void InputArray (int[] a)

 void OutputArray (int[] a)

 int Sum (int[] arr)

 bool Search (int[] arr, int key)

Pass arrays as arguments to methods by

specifying the name of the array (no brackets)

 Example: InputArray(a);

Arrays are passed by reference

Individual array elements are passed by value

Trang 10

Array parameter: Example

private void showOutputButton_Click( object sender, System.EventArgs e )

Trang 11

Multidimensional Arrays (p.113)

Require two or more subscripts to identify a

particular element

Arrays that require two subscripts to identify an

element are called double-subscripted arrays

 Rectangular arrays

 Often represent tables

 Jagged Arrays ( Arrays of arrays)

 Arrays that compose jagged arrays can be of different lengths

Trang 12

Rectangular arrays

Row 0 Row 1 Row 2

Column 1

Column 0 Column 2 Column 3

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) Array name

a[2, 1]

Trang 13

Rectangular arrays (cont.)

 GetLength (int x): returns size of xth dimension in a array

 GetLength(0)  returns number of rows

 GetLength(1)  returns number of columns

DataType[,] ArrayName;

ArrayName = new DataType [rowSize, colSize];

DataType[,] ArrayName = new DataType [rowSize, colSize];

Trang 14

Jagged Arrays (Arrays of arrays)

Row 0 Row 1 Row 2

Column 1

Column 0 Column 2 Column 3

a[0, 0] a[0, 1] a[0, 2] a[0, 3]

a[1, 0] a[1, 1]

a[2, 0] a[2, 2]

Column index (or subscript) Row index (or subscript) Array name

a[2, 1]

Trang 15

Jagged Arrays (cont.)

ArrayName = new DataType [Size][ ];

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

Trang 16

Example: Rectangular arrays

Trang 17

Example: Jagged Arrays

Trang 18

Example

Trang 19

Arrays

Collection classes

Trang 20

Collection classes

Collection classes in general are used for

maintaining lists of objects, and they may expose more functionality than simple arrays

Some class in System.Collections namespace:

 ArrayList

 Hashtable

 Queue

 Stack

Trang 21

ArrayList: Properties-Methods

value) Inserts an object at the index

ArrayList; otherwise, returns false

the specified object

Trang 22

ArrayList: Example

static void Main() {

ArrayList intArray = new ArrayList ();

Random r = new Random ();

// thêm phần tử vào ArrayList

for ( int i = 0; i < 5; i++) {

Trang 23

Example: Defining Collections

Student

- id: String

- name: String

- address: String+ ToString(): String

Students

- list: ArrayList<Student>

+ Add( st: Student ) : bool+ Remove( id: String) : bool+ ToString(): String

1 0 *

Trang 24

Why Hashtable?

 Suppose you have some data, which needs to

be stored in two dimensional array and have

a link format of Key and Value

 For example, a person name and his social

security number and we can access there

data using a key

Key Value

Trang 25

Hashtable: Properties-Methods

Add (object key , object

value ) Adds an object to the Hashtable with the specified key and value into the

Hashtable

Values Gets an ICollection of all the values

stored in the Hashtable

stored in the Hashtable

Remove (object obj ) Removes the first occurrence of the

object

Count Gets the number of elements

Clear() Removes all the elements

Trang 26

Hashtable: Properties-Methods

Contains (object item ) Returns true if the specified object

is in the Hashtable; otherwise, returns false

ContainsKey(object key ) Determines whether the

Hashtable contains a specific key

ContainsValue(object

value ) Determines whether the Hashtable contains a specific value

hashTable [object key ] Gets the value with specified key

Trang 27

Hashtable: Code examples

Create new hashtable:

Hashtable exampleTable = new Hashtable();

Adding elements to the table:

Trang 28

Hashtable: Gets all data

If you want to get all keys or values of Hashtable

If you want to get all keys and values of Hashtable

IDictionaryEnumerator enumerator = simpleTable.GetEnumerator();

Trang 29

Example 1

static void Main()

{

// tạo hashtable

Hashtable hashTable = new Hashtable ();

hashTable.Add(“00440123”, ”Ngoc Thao”);

hashTable.Add(“00123001”, ”My Tien”);

hashTable.Add(“00330124”, ”Thanh Tung”);

// truy cập qua Item

Console.WriteLine(“ Gia tri tai khoa 00440123 la: {0} ”,

hashTable[“00440123”]);

}

Trang 30

Example 2

static void Main()

{

// tạo hashtable

Hashtable hashTable = new Hashtable ();

hashTable.Add(“ 00440123 ”,” Ngoc Thao ”);

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

TỪ KHÓA LIÊN QUAN