#region
Array数组
//数组:一个数组类型确定,大小确定
Array[] a = new Array[9];
int[] ae = new int[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] ae1 = new int[9];
int[,] aa = new int[3, 3] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
//获取该数组是几维数组
int ary1 = aa.Rank;
//搜索值
int ary2 = Array.BinarySearch(ae, -5);//-5不在数组中并-5<1,返回-1
//BinarySearch<T>(T[] array, T value) 可变的类型搜索
//在数组中清除指定索引
Array.Clear(ae, 1, 2);//重索引1开始,清除2个元素
//复制数组
Array.ConstrainedCopy(ae, 4, ae1, 0, 3);//从ae中的4开始,复制到ae1数组,从ae1数组0开始的3个元素
Array.Copy(ae, ae1, 3);//在ae中从0开始,复制3个元素到ae1
//根据条件来查询,一维数组,只返回bool 不过滤数组
bool ary4 = Array.Exists<int>(ae, x => x > 6);
//根据条件来查询,一维数组,返回匹配的第一个元素
int ary5 = Array.Find<int>(ae, x => x > 6);
//根据条件来查询,一维数组,返回匹配的最后一个元素
int ary6 = Array.Find<int>(ae, x => x > 6);
//根据条件来查询,一维数组,返回匹配元素的数组
int[] ary7 = Array.FindAll<int>(ae, x => x > 6);
//循环一维数组,并执行一个函数
Array.ForEach(ae, x => { int ii7 = x; });
//反转数组
Array.Reverse(ae, 2, 6);
//获取数组的值
Object ary8 = ae.GetValue(2);
//修改数组的值
ae.SetValue(5, 7);//把索引为7的值改为5
//创建一个二维数组
int[,] ae2 = (int[,])Array.CreateInstance(typeof(int), 5, 9);
//dynamic返回接受
string astr = 可返回任何类型(ae);
//ae = 可返回任何类型(ae);
#endregion
#region ArrayList集合
//ArrayList:在一个ArrayList实例中可包含多个类型,大小会自动扩展
System.Collections.ArrayList b = new System.Collections.ArrayList();
//Array数组转换ArrayList
ArrayList list = new ArrayList(new string[] { "aaa", "bbb" });
//ArrayList数组转换Array
string[] arrString = (string[])list.ToArray(typeof(string));
b = new ArrayList(ae);
//根据索引获取值
object arylist1 = b[2];
//判断只读属性,设置只读
bool arylist2 = list.IsReadOnly;
var list2 = Array.AsReadOnly<string>(arrString);
//添加元素
list.Add(2);//末尾追加
list.AddRange(new int[] { 1, 5 });//末尾追加
list.Insert(5, 66);//索引5处,插入66
list.InsertRange(4, new int[] { 11, 55 });//索引4处,插入11,55
//移除元素
list.Remove(2);//移除值为2的
list.RemoveAt(2);//移除索引为2的
list.RemoveRange(2, 2);//移除索引2处2个值
//把ArrayList复制到Array
list.CopyTo(ae2);
//包含
bool arylist3 = list.Contains(5);
//获取迭代器
IEnumerator arylist4 = list.GetEnumerator();
//获取指定索引的ArrayList
list = list.GetRange(1, 6);
把IList转换成ArrayList
//IList<string> list11 = null;
//ArrayList arylist2 = ArrayList.Adapter(list11);
//数组:一个数组类型确定,大小确定
Array[] a = new Array[9];
int[] ae = new int[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] ae1 = new int[9];
int[,] aa = new int[3, 3] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
//获取该数组是几维数组
int ary1 = aa.Rank;
//搜索值
int ary2 = Array.BinarySearch(ae, -5);//-5不在数组中并-5<1,返回-1
//BinarySearch<T>(T[] array, T value) 可变的类型搜索
//在数组中清除指定索引
Array.Clear(ae, 1, 2);//重索引1开始,清除2个元素
//复制数组
Array.ConstrainedCopy(ae, 4, ae1, 0, 3);//从ae中的4开始,复制到ae1数组,从ae1数组0开始的3个元素
Array.Copy(ae, ae1, 3);//在ae中从0开始,复制3个元素到ae1
//根据条件来查询,一维数组,只返回bool 不过滤数组
bool ary4 = Array.Exists<int>(ae, x => x > 6);
//根据条件来查询,一维数组,返回匹配的第一个元素
int ary5 = Array.Find<int>(ae, x => x > 6);
//根据条件来查询,一维数组,返回匹配的最后一个元素
int ary6 = Array.Find<int>(ae, x => x > 6);
//根据条件来查询,一维数组,返回匹配元素的数组
int[] ary7 = Array.FindAll<int>(ae, x => x > 6);
//循环一维数组,并执行一个函数
Array.ForEach(ae, x => { int ii7 = x; });
//反转数组
Array.Reverse(ae, 2, 6);
//获取数组的值
Object ary8 = ae.GetValue(2);
//修改数组的值
ae.SetValue(5, 7);//把索引为7的值改为5
//创建一个二维数组
int[,] ae2 = (int[,])Array.CreateInstance(typeof(int), 5, 9);
//dynamic返回接受
string astr = 可返回任何类型(ae);
//ae = 可返回任何类型(ae);
#endregion
#region ArrayList集合
//ArrayList:在一个ArrayList实例中可包含多个类型,大小会自动扩展
System.Collections.ArrayList b = new System.Collections.ArrayList();
//Array数组转换ArrayList
ArrayList list = new ArrayList(new string[] { "aaa", "bbb" });
//ArrayList数组转换Array
string[] arrString = (string[])list.ToArray(typeof(string));
b = new ArrayList(ae);
//根据索引获取值
object arylist1 = b[2];
//判断只读属性,设置只读
bool arylist2 = list.IsReadOnly;
var list2 = Array.AsReadOnly<string>(arrString);
//添加元素
list.Add(2);//末尾追加
list.AddRange(new int[] { 1, 5 });//末尾追加
list.Insert(5, 66);//索引5处,插入66
list.InsertRange(4, new int[] { 11, 55 });//索引4处,插入11,55
//移除元素
list.Remove(2);//移除值为2的
list.RemoveAt(2);//移除索引为2的
list.RemoveRange(2, 2);//移除索引2处2个值
//把ArrayList复制到Array
list.CopyTo(ae2);
//包含
bool arylist3 = list.Contains(5);
//获取迭代器
IEnumerator arylist4 = list.GetEnumerator();
//获取指定索引的ArrayList
list = list.GetRange(1, 6);
把IList转换成ArrayList
//IList<string> list11 = null;
//ArrayList arylist2 = ArrayList.Adapter(list11);
#endregion
public dynamic 可返回任何类型(object a)
{
string str = a.ToString();
return str;
//return a;
}
作者:FengYang
博客地址:http://blog.csdn.net/qq_28140365
版权声明:如果感觉文章哪里写的不对或者存在疑问,欢迎留言,共同学习、进步!
蓝色字体为链接