intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài giảng Lập trình C# 2010: Chương 4 - ĐH Công nghệ Đồng Nai

Chia sẻ: Na Na | Ngày: | Loại File: PPTX | Số trang:69

110
lượt xem
17
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Bài giảng Lập trình C# 2010: Chương 4 trình bày về Using Arrays and Collections (Sử dụng mảng và Bộ sưu tập) như khái niệm, đặc điểm, cú pháp, thao tác thực hiện và các nội dung khác.

Chủ đề:
Lưu

Nội dung Text: Bài giảng Lập trình C# 2010: Chương 4 - ĐH Công nghệ Đồng Nai

  1. DONG NAI UNIVERSITY OF TECHNOLOGY
  2. DONG NAI UNIVERSITY OF TECHNOLOGY 1 One Dimension Array 2 Multi Dimension Array 3 Array class 4 ArrayList class 5 Dictionary class
  3. DONG NAI UNIVERSITY OF TECHNOLOGY One Dimension Array M[0] M[1] M[2] M[3] M[4] M[5] M[6] 5 2 9 7 6 0 8 - Same name - Same type - Get Element by Index ( 0 n-1)
  4. DONG NAI UNIVERSITY OF TECHNOLOGY Declaring & Creating Arrays • Programmer specifies the type of the elements of the array • new operator to allocate dynamically the number of elements in the array • Array declarations and initializations need not be in the same statement • In arrays of value types, each element contains one value of the declared type
  5. DONG NAI UNIVERSITY OF TECHNOLOGY Declaring: [ ] ; int [ ] M; Button[ ] arrButton; Creating: ArrayName> =new [ ARRAY_SIZ M=new int[10]; Button []arrButton=new Button[10];
  6. DONG NAI UNIVERSITY OF TECHNOLOGY Examples… int M1[]=new int[10]; for(int i=0;i
  7. DONG NAI UNIVERSITY OF TECHNOLOGY Properties & Methods • array_name.Length: return size of array • array_name.GetValue (int index): return an object at position index. • array_name[int index]: return an object at position index. • array_name.SetValue (object value, int index): add or modify an object at position index • array_name[int index] = value: add or modify an object at position index
  8. DONG NAI UNIVERSITY OF TECHNOLOGY Passing Arrays to Method • Pass arrays as arguments to methods by specifying the name of the array (no brackets) • Arrays are passed by reference • Individual array elements are passed by value
  9. DONG NAI UNIVERSITY OF TECHNOLOGY Passing Arrays to Method static void inputArray(int[] arr) { Random ran = new Random(); for (int i = 0; i
  10. DONG NAI UNIVERSITY OF TECHNOLOGY Using out or ref keyword to pass by reference static void edit(out int n) { n = -113; } OR: static void edit(ref int n) { n = -113; }
  11. DONG NAI UNIVERSITY OF TECHNOLOGY The out parameter must be assigned to before control leaves the current method static void edit(out int n) { //n = -113; }  Compile error
  12. DONG NAI UNIVERSITY OF TECHNOLOGY arr[0]=100; Use out keyword: edit(out arr[0]); Use ref keyword: edit(ref arr[0]); Two ways: arr[0] -113
  13. DONG NAI UNIVERSITY OF TECHNOLOGY 7 2 9 0 9 5 4 1 8 0 3 6 Multi Dimension
  14. DONG NAI UNIVERSITY OF TECHNOLOGY Column 0 Column 1 Column 2 Column 3 M[0, 0] M[0, 1] M[0, 2] M[0, 3] Row 0 7 2 9 0 M[1, 0] M[1, 1] M[1, 2] M[1, 3] Row 1 9 5 4 1 M[2, 0] M[2, 1] M[2, 2] M[2, 3] Row 2 8 0 3 6 Column index (or subscript) Row index (or subscript) Array name
  15. DataType[ , ] arrName; DONG NAI UNIVERSITY OF TECHNOLOGY arrName = new DataType [rowSize, colSize]; DataType [ , ] arrName = new DataType [rowSize, colSize]; int[,] M = new int[2, 2]; M[0, 0] = 1; M[0, 1] = 2; M[1, 0] = 5; M[1, 1] = 3; int[,] M = { {1,8} , {2,5} };
  16. DONG NAI UNIVERSITY OF TECHNOLOGY GetLength(int dimension) int[,] M = {{1,8},{2,5},{1,1} }; row=M.GetLength(0); row=3 col=M.GetLength(1); col=2 int[, ,] M2 = new Int32[3, 4, 5]; x=M2.GetLength(0);x= 3 y=M2.GetLength(1);y= 4 z=M2.GetLength(2);z=
  17. DONG NAI UNIVERSITY OF TECHNOLOGY Examples for (int i = 0; i < M.GetLength(0); i++) { for (int j = 0; j < M.GetLength(1); j+ +) { Console.Write(M[i,j] +" "); } Console.WriteLine(); }
  18. DONG NAI UNIVERSITY OF TECHNOLOGY Arrays of Arrays When we want to create a multi Array with different size: - The first, we declare size of row, so each row will hold an array with any size. - The second, We will declare those array - And Initialize value for new item in DataType[ ][ ] arrName = each array DataType[Size][ ];
  19. DONG NAI UNIVERSITY OF TECHNOLOGY int[][] K = new int[3][]; K[0] = new int[] { 1,3}; K[1] = new int[] { 9}; K[2] = new int[] { 5, 4, 9 }; for (int i = 0; i < K.GetLength(0); i+ +) { for (int j = 0; j < K[i].Length; j+ +) { Console.Write(K[i][j] +" "); } Console.WriteLine(); }
  20. DONG NAI UNIVERSITY OF TECHNOLOGY Array Class Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2