Chapter 6 Introduction to Arrays And String Arrays

  • Slides: 26
Download presentation
Chapter 6 Introduction to Arrays And String

Chapter 6 Introduction to Arrays And String

Arrays? Khi nào dùng Array • List or series of values all referenced by

Arrays? Khi nào dùng Array • List or series of values all referenced by the same name and type • Similar to list of values for list boxes and combo boxes - without the box • Use an array to keep a series of variable for later processing such as – Reordering – Calculating – Printing 8 - 2 2011

Array Terms • Element – Individual item in the array • Index (or subscript)

Array Terms • Element – Individual item in the array • Index (or subscript) – Zero based number used to reference the specific elements in the array – Must be an integer • Boundaries – Lower Subscript, 0 by default – Upper Subscript 8 - 3 2011

Simple Array Example (p 353) str. Name Array [0] [1] (2) (3) (4) (5)

Simple Array Example (p 353) str. Name Array [0] [1] (2) (3) (4) (5) (6) (7) (8) (9) 8 - 4 Janet Baker George Lee Sue Li Samuel Hoosier Sandra Weeks William Macy Andy Harrison Ken Ford Denny Franks Shawn James 2011

Declaring and Creating Arrays p 355 Cách 1 int[] c = new int[ 12

Declaring and Creating Arrays p 355 Cách 1 int[] c = new int[ 12 ]; Cách 2 int[] c; // declare the array variable c = new int[ 12 ]; // create the array; assign to array variable Cách 2 tương đương cách 1. string[] b = new string[ 100 ]; // create string array b string[] x = new string[ 27 ]; // create string array x 8 - 5 2011

Using an Array Initializer • Optionally, the elements in the array may be assigned

Using an Array Initializer • Optionally, the elements in the array may be assigned values in the statement int[] n = { 10, 20, 30, 40, 50 }; 8 - 6 Index Value 0 10 1 20 2 30 3 40 4 50 2011

Using an Array Initializer Index Value string[] faces = { "Ace", "Deuce", "Three", "Four",

Using an Array Initializer Index Value string[] faces = { "Ace", "Deuce", "Three", "Four", "Five", "Six"}; 8 - 7 0 Ace 1 Deuce 2 Three 3 Four 4 Five 5 Six 2011

Nhâp xuất Array Nhập, xuất thực chất là gán hoặc lấy giá trị của

Nhâp xuất Array Nhập, xuất thực chất là gán hoặc lấy giá trị của array tại vị trí nào đó. Thông thường nhập xuất mảng kết hợp vòng lặp. Ví dụ nhập mảng: int [] A= new int [50]; Random n = new Random(); int i=0; while (i < A. Length) { A[i] = n. Next(1, 91); // nhập cho item thứ i i++; } 8 - 8 2011

Nhâp xuất Array Ví dụ truy xuất Array: long t = 0; while (i

Nhâp xuất Array Ví dụ truy xuất Array: long t = 0; while (i < A. Length) { t =t+ A[i]; i++; } 8 - 9 2011

Nhâp xuất Array Truy xuất các phần tử trong array dùng cấu trúc lặp

Nhâp xuất Array Truy xuất các phần tử trong array dùng cấu trúc lặp for each rất hiệu quả. Syn tax foreach ( type identifier in array. Name ) statement 8 - 10 2011

Nhâp xuất Array Truy xuất qua vị trí Truy xuất qua foreach long tongmang(int[]

Nhâp xuất Array Truy xuất qua vị trí Truy xuất qua foreach long tongmang(int[] A) { int i = 0, x; long t = 0; while (i < A. Length) { x = A[i]; t += x; i++; } return t; } long tongmang. Foreach(int[] A) { long t = 0; foreach (int x in A) { t += x; } return t; } 8 - 11 2011

Multidimensional Arrays • Multidimensional arrays with two dimensions are often used to represent tables

Multidimensional Arrays • Multidimensional arrays with two dimensions are often used to represent tables of values consisting of information arranged in rows and columns 8 - 12 2011

Rectangular Arrays 8 - 13 2011

Rectangular Arrays 8 - 13 2011

declared and initialized <Kiểu dữ liệu> [ , ] <Tên mảng> = new <Kiểu

declared and initialized <Kiểu dữ liệu> [ , ] <Tên mảng> = new <Kiểu dử liệu> [<số dòng> , <số cột> ] Khởi gán mảng hai chiều chữ nhật: int[ , ] b = { { 7, 2 }, { 7 5, 8 } } 5 8 - 14 b[1, 0]=? 2 8 2011

Truy xuất phần tử của mảng hai chiều chữ nhật < Tên mảng >

Truy xuất phần tử của mảng hai chiều chữ nhật < Tên mảng > [ i , j ] trong đó I là chỉ số dòng, j là chỉ số cột Ví dụ : X=A[4, 5]; static void Print. Array(int[, ] a) { Console. Write. Line(); for (int i = 0; i < a. Get. Length(0); i++) { for (int j = 0; j < a. Get. Length(1); j++) Console. Write(" {0}", a[i, j]); Console. Write. Line(); } } 8 - 15 2011

Truy xuất phần tử của mảng hai chiều chữ nhật < Tên mảng >

Truy xuất phần tử của mảng hai chiều chữ nhật < Tên mảng > [ i , j ] trong đó I là chỉ số dòng, j là chỉ số cột Ví dụ : X=A[4, 5]; int Tong. Mang(int[, ] a)//tong mang 2 chieu { long T=0; for (int i = 0; i < a. Get. Length(0); i++) for(int j = 0; j < a. Get. Length(1); j++) T=T+a[i, j]; return T; } 8 - 16 2011

Mảng Jagged (Mảng lởm chởm) • Mảng Jagged là mảng mà mỗi phần tử

Mảng Jagged (Mảng lởm chởm) • Mảng Jagged là mảng mà mỗi phần tử là một mảng khác. Và hiển nhiên trong mảng jagged số cột trong các dòng sẽ không bằng nhau. 8 - 17 2011

Khai Báo Và Khởi Tạo Mảng Jagged Khai báo mảng Jagged: < Kiểu dũ

Khai Báo Và Khởi Tạo Mảng Jagged Khai báo mảng Jagged: < Kiểu dũ liệu > [ ] < Tên mảng > Khởi tạo mảng jagged: < Tên mảng > = new <Kiểu dữ liệu> [số dòng của mảng ] [ ]; -Trong quá trình nhập giá trị số dòng cho mảng chúng ta sẽ nhập số cột tương ứng cho mỗi dòng. int[][] jagged = { new int[] { 1, 2 }, new int[] { 3 }, new int[] { 4, 5, 6 } }; int[][] c; c = new int[ 2 ][ ]; // create 2 rows c[ 0 ] = new int[ 5 ]; // create 5 columns for row 0 c[ 1 ] = new int[ 3 ]; // create 3 columns for row 1 8 - 18 2011

String s = "nguyen van thang"; s= System. Threading. Thread. Current. Culture. Text. Info.

String s = "nguyen van thang"; s= System. Threading. Thread. Current. Culture. Text. Info. Title. Ca se(s. To. Lower()); //doi thanh proper 8 - 19 2011

Fundamental of Characters and Strings • Importance of characters – Character constants • Character

Fundamental of Characters and Strings • Importance of characters – Character constants • Character code (ex. 122 ‘z’, 10 ’n’) • Unicode character set (see Appendix G, Unicode) – String • Consist of characters • Object of class String in System namespace • using String to refer to the class String and string to refer to an object of class String 8 - 20 2011

string output; string original. String, string 1, string 2, string 3, string 4; char[]

string output; string original. String, string 1, string 2, string 3, string 4; char[] character. Array = { 'b', 'i', 'r', 't', 'h', 'd', 'a', 'y' }; // string initialization original. String = "Welcome to C# programming!"; string 1 = original. String; string 2 = new string( character. Array ); string 3 = new string( character. Array, 6, 3 ); string 4 = new string( 'C', 5 ); output = "string 1 = " + """ + string 1 + ""n" + "string 2 = " + """ + string 2 + ""n" + "string 3 = " + """ + string 3 + ""n" + "string 4 = " + """ + string 4 + ""n"; 8 - 21 2011

String Methods • Length property – Returns the length of the string • Copy.

String Methods • Length property – Returns the length of the string • Copy. To – Copies specified number of characters into a char array Copy. To ( int source. Index, array[] destination, int destination. Index, int count ) Copy To: tại 1 ví trên chuỗi gốc (source. Index, ), lấy một số ký tự (count), Copy tới một vị trí (destination. Index) , trên một bảng dãy ký tự Unicode destination; 8 - 22 © 10/18/2011.

String Methods • String comparison – Greater than (1) – Less than (-1) –

String Methods • String comparison – Greater than (1) – Less than (-1) – Equal (0) • Method Equals – Test objects for equality – Return a Boolean – Uses lexicographical comparison 8 - 23 © 10/18/2011.

String Methods • Index. Of methods: Reports the index of the first occurrence of

String Methods • Index. Of methods: Reports the index of the first occurrence of a String, or one or more characters, within this string. – Index. Of(char value) – Index. Of(char value, int Start. Index) – Index. Of(string value, int Start. Index) 8 - 24 © 10/18/2011.

String Methods 1. Bool My. String. Starts. With( s ) 2. Bool My. String.

String Methods 1. Bool My. String. Starts. With( s ) 2. Bool My. String. Ends. With( s ) 3. string My. String. Substring ( int start. Index, int length ) 4. My. String. To. Lower( ) 5. My. String. To. Upper( ) 6. My. String. To. String( ) 7. My. String. Trim( ) 8. My. String. Trim. End( ) 9. My. String. Trim. Start ( ) 8 - 25 © 10/18/2011.

String Methods • Method Replace (phương thức thay thế chuỗi) – Original string remain

String Methods • Method Replace (phương thức thay thế chuỗi) – Original string remain unchanged – String object. String. Replace (String old. Value, String new. Value) – String object. String. Replace (char old. Value, char new. Value) 8 - 26 © 10/18/2011.