Chapter 6 Introduction to Arrays And String
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 2011
Array Terms Element Index (or subscript) Boundaries 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 2011
Simple Array Example (p353) strName Array [0] [1] (2) (3) (4) (5) (6) (7) (8) (9) 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 p355 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 2011
Using an Array Initializer Optionally, the elements in the array may be assigned values in the statement int[] n = { 10, 20, 30, 40, 50 }; Index Value 10 1 20 2 30 3 40 4 50 2011
Using an Array Initializer Index Value Ace 1 Deuce 2 Three 3 Four 4 Five 5 Six string[] faces = { "Ace", "Deuce", "Three", "Four", "Five", "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 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++; } 2011
Nhâp xuất Array long t = 0; while (i < A.Length) { t =t+ A[i]; i++; Ví dụ truy xuất Array: long t = 0; while (i < A.Length) { t =t+ A[i]; i++; } 2011
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 arrayName ) statement 2011
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 tongmangForeach(int[] A) foreach (int x in A) 2011
Multidimensional Arrays Multidimensional arrays with two dimensions are often used to represent tables of values consisting of information arranged in rows and columns 2011
Rectangular Arrays 2011
declared and initialized <Kiểu dữ liệu> [ , ] <Tên mảng> <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 }, { 5, 8 } } b[1,0]=? 7 2 5 8 2011
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 PrintArray(int[,] a) { Console.WriteLine(); for (int i = 0; i < a.GetLength(0); i++) for (int j = 0; j < a.GetLength(1); j++) Console.Write(" {0}", a[i,j]); } 2011
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 TongMang(int[,] a)//tong mang 2 chieu { long T=0; for (int i = 0; i < a.GetLength(0); i++) for(int j = 0; j < a.GetLength(1); j++) T=T+a[i,j]; return T; } 2011
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. 2011
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 2011
String String s = "nguyen van thang"; s = System.Threading.Thread.CurrentThread .CurrentCulture.TextInfo.ToTitleCase(s.ToLower());//doi thanh proper 2011
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 2011
string output; string originalString, string1, string2, string3, string4; char[] characterArray = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' }; // string initialization originalString = "Welcome to C# programming!"; string1 = originalString; string2 = new string( characterArray ); string3 = new string( characterArray, 6, 3 ); string4 = new string( 'C', 5 ); output = "string1 = " + "\"" + string1 + "\"\n" + "string2 = " + "\"" + string2 + "\"\n" + "string3 = " + "\"" + string3 + "\"\n" + "string4 = " + "\"" + string4 + "\"\n"; 2011
String Methods Length property CopyTo Returns the length of the string Copies specified number of characters into a char array CopyTo ( int sourceIndex, array[] destination, int destinationIndex, int count ) Copy To: tại 1 ví trí trên chuỗi gốc (sourceIndex, ), lấy một số ký tự (count), Copy tới một vị trí (destinationIndex) , trên một bảng dãy ký tự Unicode destination; © 10/18/2011.
String Methods String comparison Method Equals Greater than (1) Less than (-1) Equal (0) Method Equals Test objects for equality Return a Boolean Uses lexicographical comparison © 10/18/2011.
String Methods IndexOf(char value) IndexOf(char value, int StartIndex) IndexOf methods: Reports the index of the first occurrence of a String, or one or more characters, within this string. IndexOf(char value) IndexOf(char value, int StartIndex) IndexOf(string value) IndexOf(string value, int StartIndex) © 10/18/2011.
String Methods Bool MyString.StartsWith( s ) Bool MyString.EndsWith( s ) string MyString.Substring ( int startIndex, int length ) MyString.ToLower( ) MyString.ToUpper( ) MyString.ToString( ) MyString.Trim( ) MyString.TrimEnd( ) MyString.TrimStart ( ) © 10/18/2011.
String Methods Method Replace (phương thức thay thế chuỗi) Original string remain unchanged String objectString.Replace (String oldValue, String newValue) String objectString.Replace (char oldValue, char newValue) © 10/18/2011.