1 / 54

LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG

LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG. Bài 5 Mảng- Ngọai Lệ Nội dung Khai báo mảng 1 chiều Mảng nhiều chiều Mảng răng cưa. Mảng. Ưu điểm Tổ chức quản lý tốt Cùng 1 kiểu (!!!) Thể hiện cấu trúc dữ liệu Danh sách đặc : duyệt, thêm, chèn, xóa tìm, sắp xếp nối ghép danh sách

Télécharger la présentation

LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG Bài 5 Mảng- Ngọai Lệ Nội dung Khai báo mảng 1 chiều Mảng nhiều chiều Mảng răng cưa

  2. Mảng • Ưu điểm • Tổ chức quản lý tốt • Cùng 1 kiểu (!!!) • Thể hiện cấu trúc dữ liệu Danh sách đặc : duyệt, thêm, chèn, xóa tìm, sắp xếp nối ghép danh sách trộn  Nên sử dụng các CTDL do .NET cung cấp

  3. Cú pháp khai báo mảng Khai báo mảng 1 chiều <kiểu dữ liệu> [ ] <tên mảng> ; Lưu ý - Cú pháp khác so với C/C++ - Chỉ là khai báo, chứ chưa cấp phát vùng nhớ (chưa khởi tạo)

  4. Khởi tạo mảng Nhắc lại -Có 2 trường hợp khai báo biến +Khai báo trong phương thức +Khai báo bên ngòai phương thức (nhưng nằm trong 1 lớp) -Trong C# : khai báo và khởi tạo biến ???? -Cho phép sử dụng giá trị biểu thức để mô tả kích thước mảng static int M=10; int [] xxx= new int [2*M];

  5. Khai báo bên trong 1 ph/thức Cú pháp khởi tạo <tên mảng> = new <tên kiểu dữ liệu>[sốpt]; Có thể kết hợp khai báo lẫn khởi tạo Ví dụ float [ ] f_array = new float[10]; int [] i = new int[]{2,4,6,8}; Khai báo riêng và sẽ khởi tạo trước khi sử dụng sring[] list; ……………….. list = new string[3];

  6. Khai báo bên ngòai ph.thức Do khai báo bên ngòai ph/thức nhưng nằm bên trong 1 lớp  xem như thành phần 1 lớp class ABC { string [ ] ds ; …………. } Do vậy Khai báo bên ngòai và việc khởi tạo có thể trực tiếp hay thực hiện trong 1 phương thức nào đó

  7. Ví dụ public class Class1 { int[] x_arr; float[] fx = new float[3] {2.3F, 56.2F, 6.78F } ; public Class1() { float [] f_arr= new float[10]; int [] i_arr = new int[]{2,4,6,8}; string[] list; list = new string[3]; x_arr = new int[10]; for (int i=0;i<x_arr.Length;i++) x_arr[i]=i*4; } }

  8. Xử lý mảng Thường sử dụng vòng lặp để xứ lý mảng do.. while while … for và foreach

  9. Bài tập Cho khai báo sau const int N=100; int [ ] array = new int [N]; …………………. Yêu cầu : 1- In ra các phần tử trong mảng array 2- In ra các phần tử ở vị trí chẵn trong mảng array 3- Có bao nhiêu phần tử trong mảng array có giá trị lớn hơn hay bằng X (X là 1 số kiểu int)

  10. Mảng nhiều chiều Cho phép tạo mảng nhiều chiều Cú pháp <kiểu dữ liệu> [ , …..] <tên mảng> ; Ví dụ float [ , ] this year; int [ , , ] vacumm;

  11. Yêu cầu • Thử khai báo và khởi tạo mảng thisyear chứa số thực có chiều thứ nhất là 12 phần tử, chiều thứ 2 có 31 phần tử • Thử khai báo mảng soluong chứa các số thực, mảng có 3 chiều, chiều thứ nhất có 4 phần tử, chiều thứ 2 có 10 phần tử và chiều thứ 3 có 6 phần tử

  12. Yêu cầu • Đếm và in ra số phần tử trong mảng soluong chứa giá trị là 1 số chẵn ??? • (Dùng for, while hay do ) • In ra vị trí và giá trị các phần tử trong mảng thisyear thỏa điều kiện chứa 1 số lẻ ??? (Dùng foreach)

  13. Tam giác Pascal 1- Viết chương trình in ra hàng thứ N của tgiác Pascal

  14. Mảng răng cưa (jagged array) Mảng răng cưa : là mảng mà số phần tử trong mỗi chiều có thể khác nhau Ví dụ khai báo mảng răng cưa có 2 chiều <kiểu dữ liệu> [ ] [ ] <tên mảng> ; Nhận xét :  là 1 mảng, mỗi phần tử trong mảng lại là 1 mảng khác

  15. Mảng có 2 chiều, chiều thứ nhất có 2 phần tử Phần tử thứ 1 chiều thứ nhất có 2 phần tử Phần tử thứ 2 chiều thứ nhất có 5 phần tử Yêu cầu : Thứ khai báo và khởi tạo

  16. int [ ] [ ] list ; ………. list = new int[ 2][ ] ; list[0] = new int [2] ; list [1] = new int [5] ;

  17. Yêu cầu Thực hiện khai báo,khởi tạo và gán dữ liệu là các số nguyên theo như hình vẽ vào các phần tử trong mảng (tên mảng là exam)

  18. Một số lưu ý khi sử dụng mảng - Sử dụng thuộc tính Length của mảng thay vì phải nắm số phần tử trong mảng - Cấu trúc lặp foreach hữu hiệu hơn là dùng for - Lấy số chiều 1 mảng : sử dụng thuộc tính rank

  19. Lớp Array, ArrayList Làm quen với cấu trúcdữ liệu - Lớp trừu tượng Array - Lớp ArrayList : mô phỏng danh sách đặc và dsliên kết - Lớp Queue : mô phỏng hàng đợi - Lớp Stack : mô phỏng ngăn xếp (chồng) - Lớp Hashtable

  20. Ví dụ 1: SamplesArray1.cs public static void Main() { // Tao và khoi dong mang so nguyen va Object. int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 }; Object[] myObjArray = new Object[5] { 26, 27, 28, 29, 30 }; // In ra cac gia tri trong 2 mang. Console.WriteLine( "Initially," ); Console.Write( "integer array:" ); PrintValues( myIntArray ); Console.Write( "Object array: " ); PrintValues( myObjArray );

  21. //Chep 2 phan tu dau tien cua mang so nguyen vao mang Object Array.Copy( myIntArray, myObjArray, 2 ); Console.WriteLine( "\nAfter copying the first two elements of the integer array to the Object array," ); Console.Write( "integer array:" ); PrintValues( myIntArray ); Console.Write( "Object array: " );PrintValues( myObjArray ); Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

  22. Console.WriteLine( "\nAfter copying the last two elements of the Object array to the integer array," ); Console.Write( "integer array:" ); PrintValues( myIntArray ); Console.Write( "Object array: " ); PrintValues( myObjArray ); }

  23. public static void PrintValues( Object[] myArr ) { foreach ( Object i in myArr ) Console.Write( "\t{0}", i ); Console.WriteLine(); } public static void PrintValues( int[] myArr ) { foreach ( int i in myArr ) Console.Write( "\t{0}", i ); Console.WriteLine(); }

  24. Ví dụ: SamplesArray2.cs public static void Main() { // Creates and initializes a new three-dimensional Array of type Int32. Array myArr = Array.CreateInstance( typeof(Int32), 2, 3, 4 ); for (int i=myArr.GetLowerBound(0);i<= myArr.GetUpperBound(0); i++ ) for ( int j = myArr.GetLowerBound(1); j <= myArr.GetUpperBound(1); j++ ) for ( int k = myArr.GetLowerBound(2); k <= myArr.GetUpperBound(2); k++ ) { myArr.SetValue( (i*100)+(j*10)+k, i, j, k ); }

  25. Console.WriteLine( "The Array has {0} dimension(s) and at total of {1} elements.", myArr.Rank, myArr.Length ); Console.WriteLine( "\tLength\tLower\tUpper" ); for ( int i = 0; i < myArr.Rank; i++ ) { Console.Write( "{0}:\t{1}", i, myArr.GetLength(i) ); Console.WriteLine( "\t{0}\t{1}", myArr.GetLowerBound(i), myArr.GetUpperBound(i) ); }

  26. Console.WriteLine( "The Array contains the following values:" ); PrintValues( myArr ); } The Array has 3 dimension(s) and a total of 24 elements. Length Lower Upper 0: 2 0 1 1: 3 0 2 2: 4 0 3 The Array contains the following values: 0 1 2 3 10 11 12 13 20 21 22 23 100 101 102 103 110 111 112 113 120 121 122 123 */

  27. Ví dụ : ArrayList_Insert.cs public static void Main() { ArrayList myAL = new ArrayList(); myAL.Insert( 0, "The" ); myAL.Insert( 1, "fox" ); myAL.Insert( 2, "jumps" ); myAL.Insert( 3, "over" ); myAL.Insert( 4, "the" ); myAL.Insert( 5, "dog" );

  28. // Creates and initializes a new Queue. Queue myQueue = new Queue(); myQueue.Enqueue( "quick" ); myQueue.Enqueue( "brown" ); // Displays the ArrayList and the Queue. Console.WriteLine( "The ArrayList initially contains the following:" ); PrintValues( myAL ); Console.WriteLine( "The Queue initially contains the following:" ); PrintValues( myQueue );

  29. // Copies the Queue elements to the ArrayList at index 1. myAL.InsertRange( 1, myQueue ); // Displays the ArrayList. Console.WriteLine( "After adding the Queue, the ArrayList now contains:" ); PrintValues( myAL ); // Search for "dog" and add "lazy" before it. myAL.Insert( myAL.IndexOf( "dog" ), "lazy" ); // Displays the ArrayList. Console.WriteLine( "After adding \"lazy\", the ArrayList now contains:" ); PrintValues( myAL );

  30. // Add "!!!" at the end. myAL.Insert( myAL.Count, "!!!" ); // Displays the ArrayList. Console.WriteLine( "After adding \"!!!\", the ArrayList now contains:" ); PrintValues( myAL ); // Inserting an element beyond Count throws an exception. try { myAL.Insert( myAL.Count+1, "anystring" ); } catch ( Exception myException ) { Console.WriteLine("Exception: " + myException.ToString()); } }

  31. Ví dụ : ArrayList_Add.cs public static void Main() { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" );

  32. // Creates and initializes a new Queue. Queue myQueue = new Queue(); myQueue.Enqueue( "jumped" ); myQueue.Enqueue( "over" ); myQueue.Enqueue( "the" ); myQueue.Enqueue( "lazy" ); myQueue.Enqueue( "dog" ); // Displays the ArrayList and the Queue. Console.WriteLine( "The ArrayList initially contains the following:" ); PrintValues( myAL, '\t' ); Console.WriteLine( "The Queue initially contains the following:" ); PrintValues( myQueue, '\t' );

  33. //Copies the Queue elements to the end of the ArrayList. myAL.AddRange( myQueue ); // Displays the ArrayList. Console.WriteLine( "The ArrayList now contains the following:" ); PrintValues( myAL, '\t' ); }

  34. Ví dụ : ArrayList_BSearch.cs public static void Main() { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); for ( int i = 0; i <= 4; i++ ) myAL.Add( i*2 ); // Displays the ArrayList. Console.WriteLine( "The Int32 ArrayList contains the following:" ); PrintValues( myAL );

  35. // Locates a specific object that does not exist in the ArrayList. Object myObjectOdd = 3; FindMyObject( myAL, myObjectOdd ); // Locates an object that exists in the ArrayList. Object myObjectEven = 6; FindMyObject( myAL, myObjectEven ); }

  36. public static void FindMyObject( ArrayList myList, Object myObject ) { int myIndex=myList.BinarySearch( myObject ); if ( myIndex < 0 ) Console.WriteLine( "The object to search for ({0}) is not found. The next larger object is at index {1}.", myObject, ~myIndex ); else Console.WriteLine( "The object to search for ({0}) is at index {1}.", myObject, myIndex ); }

  37. The Int32 ArrayList contains the following: 0 2 4 6 8 The object to search for (3) is not found. The next larger object is at index 2. The object to search for (6) is at index 3.

  38. Ví dụ ArrayList_Prop.cs ArrayList_Remove.cs ArrayList_Sort.cs

More Related