Top Banner
Chapter 6 Introduction to Arrays And String
26
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter08aarray string

Chapter 6Introduction to

Arrays And String

Page 2: Chapter08aarray string

20118- 2

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

Page 3: Chapter08aarray string

20118- 3

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

Page 4: Chapter08aarray string

20118- 4

Simple Array Example (p353)strName Array

[0][1](2)(3)(4)(5)(6)(7)(8)(9)

Janet BakerGeorge LeeSue LiSamuel HoosierSandra WeeksWilliam MacyAndy HarrisonKen FordDenny FranksShawn James

Page 5: Chapter08aarray string

20118- 5

Declaring and Creating Arrays p355Cá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

Page 6: Chapter08aarray string

20118- 6

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

0 10

1 20

2 30

3 40

4 50

Page 7: Chapter08aarray string

20118- 7

Using an Array Initializer

string[] faces = { "Ace", "Deuce", "Three", "Four", "Five", "Six"};

Index Value

0 Ace

1 Deuce

2 Three

3 Four

4 Five

5 Six

Page 8: Chapter08aarray string

20118- 8

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++; }

Page 9: Chapter08aarray string

20118- 9

Nhâp xuất Array

Ví dụ truy xuất Array:

long t = 0;

while (i < A.Length)

{

t =t+ A[i];

i++;

}

Page 10: Chapter08aarray string

20118- 10

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

Page 11: Chapter08aarray string

20118- 11

Nhâp xuất ArrayTruy xuất qua vị trí Truy xuất qua foreachlong 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)

{

long t = 0;

foreach (int x in A)

{

t += x;

}

return t;

}

Page 12: Chapter08aarray string

20118- 12

Multidimensional Arrays• Multidimensional arrays with two

dimensions are often used to represent tables of values consisting of information arranged in rows and columns

Page 13: Chapter08aarray string

20118- 13

Rectangular Arrays

Page 14: Chapter08aarray string

20118- 14

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 } }

7 2

5 8

b[1,0]=?

Page 15: Chapter08aarray string

20118- 15

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]); Console.WriteLine(); }

}

Page 16: Chapter08aarray string

20118- 16

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; }

Page 17: Chapter08aarray string

20118- 17

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.

Page 18: Chapter08aarray string

20118- 18

Khai Báo Và Khởi Tạo Mảng JaggedKhai 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 rowsc[ 0 ] = new int[ 5 ]; // create 5 columns for row 0c[ 1 ] = new int[ 3 ]; // create 3 columns for row 1

Page 19: Chapter08aarray string

20118- 19

String

String s = "nguyen van thang";

s = System.Threading.Thread.CurrentThread

.CurrentCulture.TextInfo.ToTitleCase(s.ToLower());//doi thanh proper

Page 20: Chapter08aarray string

20118- 20

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

Page 21: Chapter08aarray string

20118- 21

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";

Page 22: Chapter08aarray string

String Methods

• Length property – Returns the length of the string

• CopyTo– Copies specified number of characters into a

char array

© 10/18/2011.8- 22

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;

Page 23: Chapter08aarray string

String Methods

• String comparison– Greater than (1)– Less than (-1)– Equal (0)

• Method Equals– Test objects for equality– Return a Boolean– Uses lexicographical comparison

© 10/18/2011.8- 23

Page 24: Chapter08aarray string

String Methods

• 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.8- 24

Page 25: Chapter08aarray string

String Methods1. Bool MyString.StartsWith( s )

2. Bool MyString.EndsWith( s )

3. string MyString.Substring ( int startIndex, int length )

4. MyString.ToLower( )

5. MyString.ToUpper( )

6. MyString.ToString( )

7. MyString.Trim( )

8. MyString.TrimEnd( )

9. MyString.TrimStart ( ) © 10/18/2011.8- 25

Page 26: Chapter08aarray string

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.8- 26