//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
Trang 1LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG
Trang 3Cú 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)
Trang 4Khở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];
Trang 5Khai 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];
Trang 6Khai 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
Trang 7float [] 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;
}
}
Trang 8Xử lý mảng
Thường sử dụng vòng lặp để xứ lý mảngdo while
while …
for
và
foreach
Trang 12Yê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)
Trang 13Tam giác Pascal
Trang 14Mả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
Trang 15Mả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
Trang 16int [ ] [ ] list ;
………
list = new int[ 2][ ] ;list[0] = new int [2] ;list [1] = new int [5] ;
Trang 19Lớ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
Trang 23Ví 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.
Trang 24//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 );
Trang 25Console.WriteLine( "\nAfter copying the last two elements of the Object array to the integer
Trang 26public static void PrintValues( Object[] myArr ) {
foreach ( Object i in myArr )
Trang 27Ví 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 );
}
Trang 28Console.WriteLine( "The Array has {0} dimension(s) and at total of {1} elements.", myArr.Rank, myArr.Length );
Trang 29
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
Trang 33Ví dụ : ArrayList_Insert.cs
public static void Main() {
ArrayList myAL = new ArrayList(); myAL.Insert( 0, "The" );
Trang 34// 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 );
Trang 35// 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 );
Trang 36// Add "!!!" at the end.
myAL.Insert( myAL.Count, "!!!" );
// Displays the ArrayList.
Console.WriteLine( "After adding \"!!!\", the ArrayList now contains:" );
Trang 37Ví 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" );
Trang 38// Creates and initializes a new Queue.
Queue myQueue = new Queue();
// 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' );
Trang 39//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' );
}
Trang 40Ví 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 );
Trang 41// 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 );
}
Trang 42public 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 );
}
Trang 43The Int32 ArrayList contains the following:
Trang 44Ví dụ
ArrayList_Prop.cs
ArrayList_Remove.cs ArrayList_Sort.cs
Trang 54Kế họach tuần sau :