YOMEDIA
ADSENSE
Chương 6: Array, Collection Types, and Iterators
71
lượt xem 5
download
lượt xem 5
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
This chapter covers the various array and collection types available in C#. You can create two types of multidimensional arrays, as well as your own collection types while utilizing collection-utility classes.
AMBIENT/
Chủ đề:
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Chương 6: Array, Collection Types, and Iterators
- Chapter 6. Arrays, Collection Types, and Iterators Hoang Anh Viet VietHA@it-hut.edu.vn HaNoi University of Technology 1
- Objectives “This chapter covers the various array and collection types available in C#. You can create two types of multidimensional arrays, as well as your own collection types while utilizing collection-utility classes. You’ll see how to define forward, reverse, and bidirectional iterators using the new iterator syntax introduced in C# 2.0, so that your collection types will work well with foreach statements.” Microsoft 2
- Roadmap 6.1. Introduction to Arrays 6.2. Multidimentional Rectangular Arrays 6.3. Multidimentional Jagged Arrays 6.4. Collection Types 6.5. Iterators 6.6. Collection Initializers Microsoft 3
- 6.1. Introduction to Arrays Overview Implicitly Typed Arrays Type Convertibility and Covariance Arrays As Parameters (and Return Values) The System.Array Base Class Microsoft 4
- Overview An array is a set of data items, accessed using an numerical index An array is a group of contiguous memory locations that all have the same name and type Arrays are reference types, and static void SimpleArrays() the memory for the array is { allocated on the managed heap. Console.WriteLine("=> Simple Array Creation."); // Create and fill an array of 3 Integers Array Initialization Syntax: int[] myInts = new int[3]; myInts[0] = 100; Example: myInts[1] = 200; 100 myInts[0] myInts[2] = 300; // Now print each value. Position number of the element within array myInts foreach(int i in myInts) 200 myInts[1] Console.WriteLine(i); Console.WriteLine(); 300 } myInts[2] Microsoft 5
- Figure 6-1: Array of reference types versus value types Microsoft 6
- Implicitly Typed Arrays using System; public class EntryPoint C# 3.0 introduces an { abbreviated way of initializing static void Main() arrays when the type of the { array can be inferred at // A conventional array int[] conventionalArray = new int[] { 1, 2, 3 }; runtime // An implicitly typed array When the compiler is var implicitlyTypedArray = new [] { 4, 5, 6 }; presented with multiple types Console.WriteLine( implicitlyTypedArray.GetType() ); within the initialization list of an // An array of doubles implicitly typed array, it var someNumbers = new [] { 3.1415, 1, 6 }; Console.WriteLine( someNumbers.GetType() ); determines a type that all the // Won’t compile! given types are implicitly // var someStrings = new [] { "int", convertible to. // someNumbers.GetType() }; Example: } } Microsoft 7
- Type Convertibility and Covariance using System; public class Animal { } When declaring an array to public class Dog : Animal { } contain instances of a certain public class Cat : Animal { } type, the instances that may public class EntryPoint place in that array can actually Dog and Cat are { type-convertible to be instances of a more derived Animal static void Main() type. { Arrays are covariant Dog[] dogs = new Dog[3]; Example: Cat[] cats = new Cat[2]; Animal[] animals = dogs; Animal[] moreAnimals = cats; } } Microsoft 8
- Arrays As Parameters (and Return Values) static void PrintArray(int[] myInts) { for(int i = 0; i < myInts.Length; i++) Once you have created an Console.WriteLine("Item {0} is {1}", i, myInts[i]); } array, you are free to pass it as static string[] GetStringArray() a parameter and receive it as a { member return value string[] theStrings = { "Hello", "from", "GetStringArray" }; return theStrings; } static void PassAndReceiveArrays() { Console.WriteLine("=>Arrays as params and return values."); . int[] ages = {20, 22, 23, 0} ; Pass array as parameter PrintArray(ages); string[] strs = GetStringArray(); foreach(string s in strs) Get array as return value. Console.WriteLine(s); Console.WriteLine(); } Microsoft 9
- The System.Array Base Class The System.Array type houses the fundamental methods and properties that are essential to an array Some members: Clear() : set a range of elements in the array to empty values. CopyTo() : copy elements from the source array into the destination array Length : return the number of items Rank : return the number of dimensions Reverse() : reverse the contents of a one-dimensional array Sort() : sort a one-dimensional array fo intrinsic types Example: Microsoft 10
- using System; namespace ConsoleApplication11 { class Program { static void Main() { SystemArrayFunctionality(); Initialize Console.ReadLine(); items at } startup static void SystemArrayFunctionality() { Console.WriteLine("=> Working with System.Array."); . string[] gothicBands = { "Tones on Tail", "Bauhaus", "Sisters of Mercy" }; // Print out names in declared order. Console.WriteLine(" -> Here is the array:"); for (int i = 0; i
- Console.WriteLine("\n"); Array.Reverse(gothicBands); Console.WriteLine(" -> The reversed array"); // ... and print them. Reverse for (int i = 0; i
- Roadmap 6.1. Introduction to Arrays 6.2. Multidimentional Rectangular Arrays 6.3. Multidimentional Jagged Arrays 6.4. Collection Types 6.5. Iterators 6.6. Collection Initializers Microsoft 13
- 6.2. Multidimensional Rectangular Arrays using System; Declaration public class EntryPoint Each row contains the same { number of columns static void Main() { Explicit Don’t need the size of each dimension int[,] twoDim1 = new int[5, 3]; dimension when declaring the size int[,] twoDim2 = { {1, 2, 3}, type {4, 5, 6}, When creating an instance of {7, 8, 9} }; the array type, you must foreach (int i in twoDim2) Figured out provide the size of the base on { dimensions. initialization Console.WriteLine(i); expression Get the count of each } dimension: Array.GetLength } } Microsoft 14
- Roadmap 6.1. Introduction to Arrays 6.2. Multidimentional Rectangular Arrays 6.3. Multidimentional Jagged Arrays 6.4. Collection Types 6.5. Iterators 6.6. Collection Initializers Microsoft 15
- 6.3 Multidimensional Jagged Arrays A jagged array is an array of arrays. Each of the rows need not be the same size as all the others, a graphical representation of the array would not be square. Create a jagged array: Declaration Declare the number of rows in array. Each row will hold an array, which can be of any length. int[][] jagged = new int[3][]; These arrays must each = new int[] { 1, 2 }; jagged[0] be declared. Then fill in the values for=the elements 3, 4, 5 }; "inner" arrays. jagged[1] new int[] { 1, 2, in these Has different size jagged[2] = new int[] { 6, 5, 4 }; foreach (int[] ar in jagged) Microsoft 16
- for (int i = 0; i < jagged.Length; ++i) { StringBuilder sb = new StringBuilder(); for (int j = 0; j < jagged[i].Length; ++j) Use for statement { sb.AppendFormat("{0} ", jagged[i][j]); } Console.WriteLine(sb.ToString()); } foreach (int[] ar in jagged) { StringBuilder sb = new StringBuilder(); Use foreach statement foreach (int n in ar) { sb.AppendFormat("{0}", n); } Console.WriteLine(sb.ToString()); } Microsoft 17
- using System; 1. using System.Text; 2. public class EntryPoint 3. { 4. static void Main() 5. { 6. int[][] jagged = new int[3][]; 7. jagged[0] = new int[] { 1, 2 }; 8. jagged[1] = new int[] { 1, 2, 3, 4, 5 }; 9. jagged[2] = new int[] { 6, 5, 4 }; 10. foreach (int[] ar in jagged) 11. { 12. StringBuilder sb = new StringBuilder(); 13. foreach (int n in ar) 14. { 15. sb.AppendFormat("{0} ", n); 16. } 17. Console.WriteLine(sb.ToString()); 18. } 19. Console.WriteLine(); 20. for (int i = 0; i < jagged.Length; ++i) 21. { 22. StringBuilder sb = new StringBuilder(); 23. for (int j = 0; j < jagged[i].Length; ++j) 24. { 25. sb.AppendFormat("{0} ", jagged[i][j]); 26. } 27. Console.WriteLine(sb.ToString()); 28. } 29. } 30. } 31. Microsoft 18
- Roadmap 6.1. Introduction to Arrays 6.2. Multidimentional Rectangular Arrays 6.3. Multidimentional Jagged Arrays 6.4. Collection Types 6.5. Iterators 6.6. Collection Initializers Microsoft 19
- 6.4. Collection Types Ever since its inception, the .NET Framework has offered a host of collection types for managing everything from an expandable array via ArrayList, a Queue, a Stack, or even a dictionary via the HashTable class. Over the years, newer version of the .NET Framework expanded these types. Generally, a collection is any type that holds on to a set of objects and implements IEnumerable or IEnumerable. The objects in the set are typically related to each other in some way defined by the problem domain. Microsoft 20
ADSENSE
CÓ THỂ BẠN MUỐN DOWNLOAD
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
AANETWORK
TRỢ GIÚP
HỖ TRỢ KHÁCH HÀNG
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn