首页 > 代码库 > C# Array类 【温故而知新】
C# Array类 【温故而知新】
简单的数组知识代码及注释讲解
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Array类 { class Program { static void Main(string[] args) { //Array是一个抽象类,不能使用构造函数来创建数组 //创建数组 Array arr = Array.CreateInstance(typeof(int), 5);//创建类型为int,大小为5的数组。 //设置对应元素的值SetValue() for (int i = 0; i < 5; i++) { arr.SetValue(33, i); } //读取对应元素的值 for (int i = 0; i < 5; i++) { Console.WriteLine(arr.GetValue(i)); } //强制转化 int[] arrInt = (int[])arr; //创建多维数组和不基于0的数组,下面创建一个包含2*3个元素的二维数组。第一维基于1,第二维基于10 int[] lengths = { 2, 3 }; int[] lowerBounds = { 1, 10 }; Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds); racers.SetValue(new Person { FirstName = "aa", LastName = "bb" }, 1, 10); racers.SetValue(new Person { FirstName = "cc", LastName = "dd" }, 1, 11); racers.SetValue(new Person { FirstName = "ee", LastName = "ff" }, 1, 12); racers.SetValue(new Person { FirstName = "gg", LastName = "hh" }, 2, 10); racers.SetValue(new Person { FirstName = "ii", LastName = "jj" }, 2, 11); racers.SetValue(new Person { FirstName = "kk", LastName = "ll" }, 2, 12); //复制数组: //因为数组是引用类型,所以将一个数组变量赋予给另一个数组变量,就会得到两个引用同一数组的变量。 //而复制数组,会使数组实现ICloneable接口,这个接口定义的Clone()方法会创建数组的浅表副本。 //如果数组的元素是值类型,以下代码就会复制所有的值 int[] arrClone = { 1, 2 }; int[] arrClone2 = (int[])arrClone.Clone(); //如果数组包含引用类型,则不复制元素,而复制引用。 Person[] p1 = { new Person { FirstName="aa", LastName="bb"}, new Person { FirstName="cc", LastName="dd"} }; Person[] p2 = (Person[])p1.Clone(); p2[0] = new Person { FirstName = "ee", LastName = "ff" }; //Clone()与Copy()区别 //1.都是创建浅表副本 //2.Clone()方法创建一个新的数组 //3.Copy()方法必须传递阶数相同且有足够元素的已有数组 //Copy()方法 int[] arr1 = { 1, 2, 3, 4 }; int[] arr2 = { 5, 6, 7, 8, 9, 10, 11 }; arr1.CopyTo(arr2, 2); //浅表副本与深层副本:浅复制就是仅复制类中的值类型成员 //深复制就是复制类中的值类型成员和引用类型的成员。 //排序:注意IComparable<Person>接口与IComparer<Person>接口的不同 //(1) string[] names = { "bb", "ff", "aa", "cc", "ee", "dd" }; Array.Sort(names); foreach (string name in names) { Console.WriteLine(name); } //对自定义类数组进行排序,需要实现IComparable接口, Person[] p3 = { new Person { FirstName="aa", LastName="bb"}, new Person { FirstName="cc", LastName="dd"}, new Person { FirstName="ee", LastName="ff"}, new Person { FirstName="dd", LastName="dd"} }; Array.Sort(p3); foreach (var p in p3) { Console.WriteLine(p.ToString()); } //(2) Person[] p4 = { new Person { FirstName="aa", LastName="bb"}, new Person { FirstName="cc", LastName="dd"}, new Person { FirstName="ee", LastName="ff"}, new Person { FirstName="dd", LastName="dd"} }; Array.Sort(p4, new PersonCompare(PersonCompareType.FirstName)); foreach (var p in p3) { Console.WriteLine(p.ToString()); } //数组作为参数 //在方法中返回数组 //static Person[] GetPersons() //{ // return new Person[] { // new Person { FirstName="aa", LastName="bb"}, // new Person { FirstName="cc", LastName="dd"}, // new Person { FirstName="ee", LastName="ff"}, // new Person { FirstName="dd", LastName="dd"} // }; //} //参数 //static void DisplayPersons(Person[] person) //{ //} //数组的协变 //说明:数组支持协变。这表示数组可以声明为基类,其派生类型的元素可以赋予数组元素。 //例如:可以声明一个object[]类型的参数,给它传递一个参数Person[] //static void DisplayPersons(object[] person) //{ //} //ArraySegment<T> int[] ar1 = { 1, 3, 5, 7, 9 }; int[] ar2 = { 2, 4, 6, 8, 10 }; var segments = new ArraySegment<int>[2] { new ArraySegment<int>(ar1,2,3), new ArraySegment<int>(ar2,1,4), }; int sum = SumOfSegments(segments); Console.ReadKey(); } static int SumOfSegments(ArraySegment<int>[] segments) { int sum = 0; foreach (var segment in segments) { int offset = segment.Offset; int count = segment.Count; for (int i = offset; i < offset + count; i++) { sum = sum + segment.Array[i]; } } return sum; } } public class Person : IComparable<Person> { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return string.Format("{0},{1}", FirstName, LastName); } public int CompareTo(Person other) { if (other == null) { return 1; } int result = string.Compare(this.LastName, other.LastName); if (result == 0) { result = string.Compare(this.FirstName, other.FirstName); } return result; } } //排序(2)使用的类 //解释说明:类PersonCompare实现了IComparable<Person>接口,可以按照FirstName或LastName对Person对象进行排序。 //枚举PersonCompareType定义了可用于PersonCompare的排序选项:FirstName和LastName。 //排序方式由PersonCompare类的构造函数定义,在该构造函数中设置了一个PersonCompareType值。 //实现Compare()方法时用一个swith语句指定是按FirstName还是LastName排序。 public enum PersonCompareType { FirstName, LastName } public class PersonCompare : IComparer<Person> { private PersonCompareType compareType; public PersonCompare(PersonCompareType compareType) { this.compareType = compareType; } public int Compare(Person x, Person y) { if (x == null && y == null) return 0; if (x == null) return 1; if (y == null) return -1; switch (compareType) { case PersonCompareType.FirstName: return string.Compare(x.FirstName, y.FirstName); case PersonCompareType.LastName: return string.Compare(x.LastName, y.LastName); default: throw new ArgumentException("unexpected compare type"); } } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。