C#中Array类创建动态类型、长度的数组
来源:互联网 发布:js中const 编辑:程序博客网 时间:2024/05/03 05:07
Array类
提供创建、操作、搜索和排序数组的方法,因而在公共语言运行时中用作所有数组的基类。
命名控件: System程序集:mscorlib语法:public abstract class Array:ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuraEquatable我们可以看出,Array是一个抽象类。在博客http://blog.csdn.net/wangshubo1989/article/details/46985269中所诉的用方括号声明的数组,其实是使用Array类的表示法。在后台使用C#语法,会创建一个派生自抽象基类Array的新类。这样就可以使用Array类为每个C#数组定义的方法和属性了。1、使用Array创建数组前面已经提到, Array是一个抽象类,所以不能使用构造函数来创建数组。但是可以使用静态方法CreateInstance()创建数组。CreateInstance()的重载版本有很多://创建使用从零开始的索引、具有指定Type和长度的一维Array
public static Array CreateInstance(Type elementType, int length)例如:
Array my1DArray = Array.CreateInstance(typeopf(Int32), 5);//创建使用从零开始的索引、具有指定Type和维长的多维Array。维的长度在一个32位整数数组中指定。public static Array CreateInstance(Type elementType, params int[] lengths)例如:int [] myLengthsArray = new int[4] {2,3,4,5};Array my2DArray = Array.CreateInstance(typeopf(string), myLengthsArray);//创建使用从零开始的索引、具有指定Type和维长的多维Array。维的长度在一个64位整数数组中指定。
public static Array CreateInstance(Type elementType, params long[] lengths)例如:int [] myLengthsArray = new int[4] {2,3,4,5};Array my3DArray = Array.CreateInstance(typeopf(string), myLengthsArray);//创建使用从零开始的索引、具有指定Type和维长的二维Array。
public static Array CreateInstance(Type elementType, int length1, int length2)例如:Array my4DArray = Array.CreateInstance(typeopf(string), 2, 3);//创建一个2*3的数组//创建使具有指定下限、指定Type和维长的多维Array。
public static Array CreateInstance(Type elementType, int [] lengths, int [] lowerBounds)其中, lengths表示要创建的Array的每个维度的大小;lowerBounds表示要创建的Array的每个维度的起始索引。例如:int [] lengths = {2, 3};int [] lowerBounds{1, 10};Array my5DArray = Array.CreateInstance(typeopf(string), lengths , lowerBounds);//创建一个2*3的数组,第一维基于1, 第二维基于10//创建使用从零开始的索引、具有指定Type和维长的三维Array。
public static Array CreateInstance(Type elementType, int length1, int length2, int length3)例如:Array my5DArray = Array.CreateInstance(typeopf(string), 2, 3, 4);2、设置和获取数组的元素
使用SetValue()函数设置数组的元素,SetValue()有许多重载的版本使用GetValue()函数来获取数组的元素,GetValue()有许多重载的版本例如:// Creates and initializes a one-dimensional array.String[] myArr1 = new String[5];// Sets the element at index 3.myArr1.SetValue( "three", 3 );Console.WriteLine( "[3]: {0}", myArr1.GetValue( 3 ) );// Creates and initializes a two-dimensional array.String[,] myArr2 = new String[5,5];// Sets the element at index 1,3.myArr2.SetValue( "one-three", 1, 3 );Console.WriteLine( "[1,3]: {0}", myArr2.GetValue( 1, 3 ) );// Creates and initializes a three-dimensional array.String[,,] myArr3 = new String[5,5,5];// Sets the element at index 1,2,3.myArr3.SetValue( "one-two-three", 1, 2, 3 );Console.WriteLine( "[1,2,3]: {0}", myArr3.GetValue( 1, 2, 3 ) );// Creates and initializes a seven-dimensional array.String[,,,,,,] myArr7 = new String[5,5,5,5,5,5,5];// Sets the element at index 1,2,3,0,1,2,3.int[] myIndices = new int[7] { 1, 2, 3, 0, 1, 2, 3 };myArr7.SetValue( "one-two-three-zero-one-two-three", myIndices );Console.WriteLine( "[1,2,3,0,1,2,3]: {0}", myArr7.GetValue( myIndices ) );}}/*上诉代码的输出:[3]: three[1,3]: one-three[1,2,3]: one-two-three[1,2,3,0,1,2,3]: one-two-three-zero-one-two-three*/3、复制数组
复制数组会使数组实现ICloneable接口,这个接口定义的Clone()方法会创建数组的浅表副本。但是如果数组的元素包含引用类型,则不复制元素,只复制引用。除了Clone()之外,还有Array.Copy方法创建浅表副本,二者的区别详见博客http://blog.csdn.net/wangshubo1989/article/details/470026674、排序
Array类使用QuickSort(快速排序)算法对数组中元素进行排序。Sort()方法需要数组中的元素实现IComparable接口。(string、int都实现了IComparable接口)同样,Sort方法有许多重载函数,下面简单介绍几个。//使用Array中每个元素的IComparable实现,对整个一维Array中的元素进行排序。public static void Sort(Array array)例如:string [] names = {"wang", "li", "liu", "zhao"};Array.Sort(names);如果对数组使用自定义类,就必须实现IComparable接口例如:Person类public class Person : IComparable {public Person() { }public Person(string name, string sex) {this.Name = name;this.Sex = sex;}public string Name;public string Sex;public override string ToString() {return this.Name + " " + this.Sex;}#region IComparable 成员public int CompareTo(object obj) {Person p = obj as Person;if (p == null) {throw new NotImplementedException();}return this.Name.CompareTo(p.Name);}#endregion} 这里就可以对Person对象数组排序了:Person[] persons = {new Person("Lili", "Female"),new Person("Heicer", "Male"),new Person("Lucy", "Female") };Array.Sort(persons);foreach (Person p in persons){Console.WriteLine(p);}有关更详细的Sort()的使用方法会在后面的Blog中更新。 0 0
- C#中Array类创建动态类型、长度的数组
- 创建数组长度可改变的动态数组
- C#动态设置数组长度
- Array对象的数组长度
- C# 中反射获取某类的子类和根据类型名动态创建对象(转载)
- 数组属性,根据参数动态设置类中数组的长度!构造函数传入数组长度!并初始化数组!
- C# 中数组和字符串的长度
- 用Java动态增加任意类型数组的长度
- Array类型、数组创建、数组检测
- C#创建动态数组
- C#动态创建数组
- C#创建动态数组
- C++中获取静态数组和动态数组的长度
- 创建String类型的数组,动态初始化数组
- c++中数组类型和长度的简单认识
- C++中char类型数组的长度问题
- 输入一个数组长度,动态创建数组,所有元素随机生成,输出元素中最大值
- C#数组类型(Array types)
- css3
- Codeforces Round #412 ( Div. 2)A+B
- 点滴生活
- 介绍write()和print()方法
- 算法学习一:二叉树的前序中序后序遍历
- C#中Array类创建动态类型、长度的数组
- Android编程:使用toolbar
- Android之aar文件
- jsp基础
- 【线段树】目录
- 小米6 Cancel by user
- 方法的修饰符
- 几类函数的求导
- 新闻发布系统轮播效果