Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 69 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
69
Dung lượng
367,28 KB
Nội dung
DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY One Dimension Array Multi Dimension Array Array class ArrayList class Dictionary class DONG NAI UNIVERSITY OF TECHNOLOGY One Dimension Array M[0] M[1] M[2] M[3] M[4] - Same name - Same type - Get Element by Index ( n-1) M[5] M[6] 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 DONG NAI UNIVERSITY OF TECHNOLOGY Declaring: [ ] ; int [ ] M; Button[ ] arrButton; Creating: =new [ ARRAY_SIZE]; M=new int[10]; Button []arrButton=new Button[10]; DONG NAI UNIVERSITY OF TECHNOLOGY Examples… int M1[]=new int[10]; for(int i=0;i[...]... a new instance of the ArrayList class that is empty and has the specified initial capacity ArrayList arrList = new ArrayList(113); DONG NAI UNIVERSITY OF TECHNOLOGY Constructors Description ArrayList Initializes a new instance of the ArrayList class (ICollection) that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied ArrayList... NAI UNIVERSITY OF TECHNOLOGY ArrayList Class Implements the IList interface using an array whose size is dynamically increased as required DONG NAI UNIVERSITY OF TECHNOLOGY Constructors Description ArrayList () Initializes a new instance of the ArrayList class that is empty and has the default initial capacity ArrayList arrList = new ArrayList(); DONG NAI UNIVERSITY OF TECHNOLOGY Constructors Description... length is specified as a 32-bit integer DONG NAI UNIVERSITY OF TECHNOLOGY int[] myArr = new int[5] { 1, 2, 3, 4, 5 }; Object[] myObjArr = new Object[5] { 26, 27, 28, 29, 30 }; Copy 2 element from myArr to myObjArr: Array.Copy(myArr,myObjArr,2) ; DONG NAI UNIVERSITY OF TECHNOLOGY Method BinarySearch(Array, Object) Searches an entire one-dimensional sorted Array for a specific element, using the IComparable... 1, 1, 4 }); foreach (int value in arr) Console.Write(value+" "); //Output : 1 1 4 DONG NAI UNIVERSITY OF TECHNOLOGY Method Description Insert Inserts an element into the ArrayList at the specified index ArrayList arr=new ArrayList(); arr.AddRange(new int[] { 1, 1, 4 }); arr.Insert(1,"teo"); foreach (object value in arr) Console.Write(value+" "); //Output : 1 teo 1 4 DONG NAI UNIVERSITY OF TECHNOLOGY... OF TECHNOLOGY Column 0 Column 1 Column 2 Column 3 M[0, 0] M[0, 1] M[0, 2] M[0, 3] 7 2 9 0 M[1, 0] M[1, 1] M[1, 2] M[1, 3] 9 5 4 1 M[2, 0] M[2, 1] M[2, 2] M[2, 3] 8 0 3 6 Row 0 Row 1 Row 2 Column index (or subscript) Row index (or subscript) Array name DONG NAI UNIVERSITY OF TECHNOLOGY DataType[ , ] arrName; arrName = new DataType [rowSize, colSize]; DataType [ , ] arrName = new DataType [rowSize, colSize];... int[] { 1, 1, 4 }); ArrayList arr2 = new ArrayList(arr1); DONG NAI UNIVERSITY OF TECHNOLOGY Method Description Add Adds an object to the end of the ArrayList ArrayList arr=new ArrayList(); arr.Add(1);arr.Add("a");arr.Add(9); foreach (object item in arr) Console.Write(item+“ ”); //Output : 1 a 9 DONG NAI UNIVERSITY OF TECHNOLOGY Method Description AddRange Adds the elements of an ICollection to the end... 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(); } 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 DONG NAI UNIVERSITY OF TECHNOLOGY Method CreateInstance(Type,...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 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 DONG NAI UNIVERSITY OF TECHNOLOGY 7 2 9 0 9 5 4 1 8 0 3 6 Multi Dimension DONG NAI UNIVERSITY... Method Description InsertRange Inserts the elements of a collection into the ArrayList at the specified index ArrayList arr=new ArrayList(); arr.AddRange(new int[] { 1, 1, 4 }); ArrayList arrIns=new ArrayList(); arrIns.Add("teo"); arrIns.Add(9); arr.InsertRange(2,arrIns); foreach (object value in arr) Console.Write(value+" "); //Output : 1 1 teo 9 4 DONG NAI UNIVERSITY OF TECHNOLOGY Method Description... teo 9 4 DONG NAI UNIVERSITY OF TECHNOLOGY Method Description Remove Removes the first occurrence of a specific object from the ArrayList ArrayList arr=new ArrayList(); arr.AddRange(new object[] { 1, "ti", 4, "ti" }); arr.Remove("ti"); arr.Remove(1); foreach (object value in arr) Console.Write(value+" "); //Output : 4 ti ... Initializes a new instance of the ArrayList class (ICollection) that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied ArrayList... NAI UNIVERSITY OF TECHNOLOGY ArrayList Class Implements the IList interface using an array whose size is dynamically increased as required DONG NAI UNIVERSITY OF TECHNOLOGY Constructors Description... ArrayList arrClone = (ArrayList)arr.Clone(); arrClone.RemoveAt(0); foreach (object val in arr) Console.Write(val + " "); //Output : 975 DONG NAI UNIVERSITY OF TECHNOLOGY Method Description CopyTo Copies