1、一般数组
定义: 数据类型[] 数组名称 = new 数据类型[可省略数组长度]{元素值}
int[] myarray = { 1, 2, 3, 4, 5, };
int[] myarray2 = new int[2] { 1, 2 };
int[] myarray3 = new int[2];
int[] myarray4 = { 1, 2 };
2、二维数组
两个参数,第一个表示用几个一般数组,第二个参数表示一般数组的个数
int[,] vs = new int[2,3];
int[,] vs2 = {
{ 1,2,3},
{ 2,3,4},
};
3、锯齿数组:内层数组元素个数不同
int[][] ged = new int[2][];
ged[0] = new int[]{ 1,2,4};
ged[1] = new int[] { 2,3,4,5,9,67};
Array:
1、一般数组创建:
Array intA = Array.CreateInstance(typeof(int),10);// 数组类型,元素个数 for (int i = 0; i < intA.Length; i++) { intA.SetValue(23, i); }
2、二维数组创建:
// // 摘要: // Creates a multidimensional System.Array of the specified System.Type and dimension // lengths, with the specified lower bounds. // // 参数: // elementType: // The System.Type of the System.Array to create. // // lengths: // A one-dimensional array that contains the size of each dimension of the System.Array // to create. // // lowerBounds: // A one-dimensional array that contains the lower bound (starting index) of each // dimension of the System.Array to create. // // 返回结果: // A new multidimensional System.Array of the specified System.Type with the specified // length and lower bound for each dimension. // int[] length = { 2, 3 }; int[] lowerBounds = { 20, 30 }; Array intB = Array.CreateInstance(typeof(int), length, lowerBounds);//数组类型,元素维度(2*3),每个维度元素最初始索引值 intB.SetValue(23, 20, 30); intB.SetValue(24, 20, 31); intB.SetValue(25, 20, 32); intB.SetValue(26, 21, 30);
3、数组排序:Array.Sort(数组)
如果需要自定义排序规则,需要实现接口IComparable,
如果相同返回0
如果需要往前派则需要返回小于0的值
如果需要往后排则需要返回大于0的值
1、自定义排序规则: 可以修改类的前提下实现IComparable接口
public class Person:IComparable<Person>
{
public string Name { get; set; }
public string Address { get; set; }
public int CompareTo(Person? other)
{
if (other == null) return 1;
int result = this.Name.CompareTo(other.Name);
return result;
}
}
Person p1 = new Person();
p1.Name = "lily";
Person p2 = new Person();
p2.Name = "ali";
Person p3 = new Person();
p3.Name = "sum";
Person[] arr = { p1, p2, p3 };
Array.Sort(arr); // ali lily sum
2、不可以修改类的时候:实现IComparer接口
//排枚举类型,用于排序的字段
public enum StudentCompareType {
FirstName,
LastName
}
//实现排序接口类
public class StudentComparer : IComparer<Student>
{
private StudentCompareType compareType;
public StudentComparer(StudentCompareType studentCompareType)
{
this.compareType = studentCompareType;
}
public int Compare(Student? x, Student? y)
{
if (x == null || y == null) return 0;
if (x == null) return 1;
if (y == null) return -1;
switch (compareType)
{
case StudentCompareType.FirstName:
return string.Compare(x.firstName, y.firstName);
case StudentCompareType.LastName:
return string.Compare(x.lastName, y.lastName);
default:
throw new NotImplementedException();
}
}
}
// 调用
Student s1 = new Student();
s1.firstName = "lily";
s1.lastName = "lily";
Student s2 = new Student();
s2.firstName = "alily";
s2.lastName = "alily";
Student s3 = new Student();
s3.firstName = "slily";
s3.lastName = "slily";
Student[] sarr = { s1, s2, s3 };
Array.Sort(sarr, new StudentComparer(StudentCompareType.FirstName));
4、 自定义foreach迭代器:使用foreach进行遍历循环
在类内部提供一个迭代器GetEnumerator , 而不必实现整个 IEnumerable接口. 当编译器检测到迭代器时, 它将自动生成 IEnumerable 或 IEnumerable<T>接口的 Current、MoveNext 和 Dispose 方法.类继承接口IEnumerable, 再提供一个迭代器GetEnumerator就可以了
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class ForeachClass
{
public string[] names { get; set; }
public ForeachClass(string[] names)
{
this.names = names;
}
public IEnumerator GetEnumerator()
{
for (int i = names.Length - 1; i >= 0; i--)
{
yield return names[i];
}
}
}
}
调用
string[] s = { "a", "b", "c", "d", "e" };
ForeachClass foreachClass = new ForeachClass(s);
foreach (var i in foreachClass)
{
Console.WriteLine(i);
}