- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- namespace CSharp基础
- {
- class 数组示例
- {
- public static void Main()
- {
- //二维数组
- int[,] arr1 = { {12,12},{12,12}};
- //锯齿数组
- int[][] arr2 = new int [3][] ;
- arr2[0] = new int[] { 12,12};
- arr2[1] = new int[] { 12, 12 };
- arr2[2] = new int[] { 12, 12 };
- //其实int[]在编译背后实则是一个Array类的子类,可以调用array中的方法,也可以显示的与array转换
- Array arr3 = Array.CreateInstance( typeof(int) , 3 );
- foreach( int i in arr3 )
- {
- arr3.SetValue(22,i) ;
- }
- //CreateInstance的重载方法可以创建多维数组
- //数组的维数 2*3
- int[] lengths = { 3, 3 };
- //数组的一维与二维的起始下标
- int[] lowerBounds = { 1 , 11 };
- Array arr4 = Array.CreateInstance( typeof(int) ,lengths , lowerBounds );
- arr4.SetValue(555 , 1 ,11 );
- arr4.SetValue(555, 1 , 12);
- arr4.SetValue(555, 1, 13);
- arr4.SetValue(555, 2, 11);
- arr4.SetValue(555, 2, 12);
- arr4.SetValue(555, 2, 13);
- arr4.SetValue(555, 3, 11);
- arr4.SetValue(555, 3, 12);
- arr4.SetValue(555, 3, 13);
- //复制数组 1 使用ICloneable, Array类是实现了ICloneable的,调用clone方法将实现浅拷贝
- int[,] arr5 = (int[,])arr4.Clone();
- foreach (int i in arr5)
- {
- Console.WriteLine(i);
- }
- //浅拷贝实现方法2
- int[,] arr6 = new int[3,3];
- Array.Copy(arr4, arr6, arr4.Length);
- foreach (int i in arr6)
- {
- Console.WriteLine(i);
- }
- //如果想实现深拷贝,必须跌代数组,创建新对象
- //数组的排序,c#中要求数组中的元素实现了IComparable接口才可以排序,string int等都是实现过的
- string[] names = { "zhao" , "aladdin" , "emep", "microsoft"};
- Array.Sort(names) ;
- foreach( string str in names )
- {
- Console.WriteLine(str);
- }
- //自定义类型实现排序
- Person[] pers = new Person[] { new Person("aladdin", 8), new Person("zhao", 12), new Person("emep", 44), new Person("ms", 2) };
- Array.Sort( pers );
- foreach( Person per in pers )
- {
- Console.WriteLine( "姓名{0} 年纪:{1}",per.name ,per.age );
- }
- //以下示例,很明显,是一个引用数组的拷贝,只是浅拷贝,copyPers[0]完全可以改变原对象的引用,这不是我们希望的
- Person[] copyPers = (Person[])pers.Clone();
- copyPers[0].name = "eidt ms";
- Console.WriteLine( pers[0].name );
- //要实现深度拷贝,是一个耗费资源的事,需要跌代,创建
- Person[] copyPers2 = new Person[pers.Length];
- for (int i = 0; i < pers.Length; i++)
- {
- copyPers2[i] = new Person( pers[i].name ,pers[i].age );
- }
- copyPers2[1].name = "eidt zhao";
- Console.WriteLine(pers[1].name);
- //从上面结果可以看出,堆中的数据也产生了副本,实现深度拷贝
- //用IComparer接品实现排序
- Array.Sort( copyPers2, new PersionComparer() );
- Console.WriteLine("------------按名称字母排序----------------------");
- foreach (Person per in copyPers2)
- {
- Console.WriteLine("姓名{0} 年纪:{1}", per.name, per.age);
- }
- Console.ReadLine();
- }
- class PersionComparer : IComparer
- {
- public int Compare( object x , object y )
- {
- Person p1 = x as Person;
- Person p2 = y as Person;
- if (p1 != null && p2 != null)
- {
- return p1.name.CompareTo( p2.name );
- }
- else
- {
- return -1;
- }
- }
- }
- class Person : IComparable
- {
- public string name;
- public int age;
- public Person(string name, int age)
- {
- this.name = name ;
- string a = "sd";
- this.age = age ;
- }
- //按年纪做比较
- public int CompareTo(object obj)
- {
- Person per = obj as Person ;
- if (per != null)
- {
- if (this.age == per.age)
- {
- return 0;
- }
- else if (this.age > per.age)
- {
- return 1;
- }
- else
- {
- return -1;
- }
- }
- return -1;
- }
- }
- }
- }
c#中的数组
最新推荐文章于 2023-05-20 22:16:33 发布