一、概述
数组为引用类型,其中的元素固定。 定义后不能增加删除元素。(如果事先不知道应包含多少元素,则应使用List集合)。数组可以包含同一类型的多个元素。
数组实现了IEumerable,ICollection,IList接口的部分功能。
二、声明数组变量及使用
1、一维数组:[]
声明及初始化:
int[] arr1 = new int[5];//声明带5个元素的数组//或者int[] arr2 = new int[] { 1, 3, 5, 7, 9 };//或者int[] arr3 = { 1, 3, 5, 7, 9 };
数组元素为引用类型:
Person[] arrPerson = new Person[2];arrPerson[0] = new Person {"a","af"};
Person[] arrPerson2 = {new Person {"a","af"},new Person {"c","cf"}}访问数组元素,为数组元素赋值:int a = arr1[0];//整数索引。
arr1[3] = 7;遍历for (int i = 0; i < arr1.Length; i++){Console.WriteLine(i + "," + arr1[i]);}//或者foreach (int item in arr2){Console.WriteLine(item);}
2、二维数组:[,]
声明及初始化:
int[,] arr1 = new int[2, 3];//两行三列int[,] arr2 = { { 1, 2, 3 }, { 4, 5, 6 } };访问数组元素,为数组元素赋值:arr1[0, 2] = 25;//为数组的第一行第三列元素赋值为25
遍历:
for (int i = 0; i < arr1.GetLength(0); i++){for (int j = 0; j < arr1.GetLength(1); j++){Console.WriteLine(i + "," + j + "," + arr1[i, j]);}}
3、锯齿数组:[][]
每一行的元素个数可以不一样,数组的数组。
声明及初始化:
int[][] arr1 = new int[][]{new int[] { 1, 2 },new int[] {3,4,5}};访问数组元素,为数组元素赋值:arr1[0] = new int[] { 1, 2 };arr1[1] = new int[] { 3, 4, 5 };
遍历:
for (int i = 0; i < arr1.Length; i++){for (int j = 0; j < arr1[i].Length; j++){Console.WriteLine(i + "," + j + "," + arr1[i][j]);}}
三、Array类
Arrary类为抽象类,用括号[]声明是C#中使用Array类的简写形式。
Array array = Array.CreateInstance(typeof(int), 5);//相当于 int[] array=new int[5];for (int i = 0; i < 5; i++){array.SetValue(33, i);}for (int i = 0; i < 5; i++){Console.WriteLine(array.GetValue(i));}Arrary类实现了以下接口:IEumerable:使用foreach语句遍历,必须实现此接口。
ICollection:IEumerable IList:ICollection ICloneable
四、Array类的成员
Length : 获取 Array 的所有维度中的元素总数。
Rank: 获取 Array 的秩(维数)。 例如,一维数组返回 1,二维数组返回 2,依次类推。
GetLength(Int32): 获取一个 32 位整数,该整数表示 Array 的指定维中的元素数。
GetLowerBound(Int32) : 获取数组中指定维度第一个元素的索引。
GetUpperBound(Int32): 获取数组中指定维度最后一个元素的索引。
五、Array类的静态方法
1、Array.Sort 排序1、使用 Array 中每个元素的 IComparable 实现,对一维 Array 中的部分元素进行排序。
public static void Sort (Array array, int index, int length);
实例:
void Main(){Person[] ArrPerson = { new Person("ab", "bb"), new Person("ca", "ab") };Array.Sort(ArrPerson);foreach (Person p in ArrPerson){Console.WriteLine(p);}}
public class Person : IComparable{public string FirstName;public string LastName;public Person(string firstName, string lastName){this.FirstName = firstName;this.LastName = lastName;}public int CompareTo(object obj){Person other = obj as Person;int result = this.LastName.CompareTo(other.LastName);//如果实例应排在参数对象前面,返回负数;如果实例应排在参数对象后面,返回整数;如果实例与参数对象相等,返回0;
if (result == 0){ result = this.FirstName.CompareTo(other.FirstName); }return result;}}2、使用指定的 IComparer,对一维 Array 中的部分元素进行排序。
comparer:比较元素时要使用的 IComparer 实现。或 若为 null,则使用每个元素的 IComparable 实现。
public static void Sort (Array array, int index, int length, System.Collections.IComparer comparer);
实例:
void Main(){Person[] ArrPerson = { new Person("ab", "bb"), new Person("ca", "ab") };Array.Sort(ArrPerson, new PersonComparer());foreach (Person p in ArrPerson){Console.WriteLine(p);}}public class Person{public string FirstName;public string LastName;public Person(string firstName, string lastName){this.FirstName = firstName;this.LastName = lastName;}}
public class PersonComparer : IComparer{public int Compare(object x, object y){Person p1 = x as Person;Person other = y as Person;int result = p1.LastName.CompareTo(other.LastName);if (result == 0){ result = p1.FirstName.CompareTo(other.FirstName); }return result;}}2、Array.Reverse(Array, Int32, Int32) 反转反转一维 Array 中某部分元素的元素顺序。
3、Array.IndexOf(Array, Object, Int32, Int32) 查找位置在一个一维数组的一系列元素中搜索指定对象,然后返回其首个匹配项的索引。 该元素系列的范围从指定数量的元素的指定索引开始。
4、Array.LastIndexOf(Array, Object, Int32, Int32)搜索指定的对象并返回一维 Array 中包含指定数目元素且在指定索引处结尾的元素范围内的最后一个匹配项的索引。
5、Array.Clear(Array, Int32, Int32) 清除元素的值将数组中的某个范围的元素设置为每个元素类型的默认值。元素个数不变6、Array.Copy(Array, Int32, Array, Int32, Int32) 复制到一个已有数组复制 Array 中的一系列元素(从指定的源索引开始),并将它们粘贴到另一 Array 中(从指定的目标索引开始)。 长度和索引指定为 32 位整数。
7、Array.Clone() 创建一个新数组创建 Array 的浅表副本。
到此这篇关于C#常用数据结构之数组Array的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:详解c# 数组(Array)
C#中数组、ArrayList、List、Dictionary的用法与区别浅析(存取数据) C#中数组、ArrayList和List三者的区别详解及实例C#中数组、ArrayList和List三者的区别详解C#中数组Array,ArrayList,泛型List详细对比浅析C#中数组,ArrayList与List对象的区别
