Array、ArrayList、List、IEnumerable、for、foreach应用

一、Array 类 (System)
声明数组(本身也是一种变量,要先声明再使用)
1.声明数组的语法,数组大小由长度绝定;
数据类型 [] 数组名;
如:
string[] student;   //字符串型数组
int[] month;        //整型数组
 
2.指定数组大小
string[] student;
student = new  string[3];            //先声明数组,再指定大小3个字符.
或者
string [] student = new  string [3]; //声明同时指定其大小为3个字符。
 
3.初始化数组
string[] student = new  string[3] { "学生" , "士兵" , "教师" };
//声明一个包含三个元素的字符串型数组,并为组中的元素赋初始值.
 
string[] student = new  string[] { "学生" , "士兵" , "教师"  };
string[] student = { "学生" , "士兵" , "教师"  };
//不指定数组的长度(大小),数组长度由打括号中的元素决定;
 
string[] student = new  string [4];
student[0] = "罗帅" ;
student[2] = "楠楠" ;
//给指定的数组下标赋值
 
4.访问数组
//数组元素的编号称为下标,下标从零开始.
 
二、Array应用
string[] ar = { "a" , "c" , "d" , "e" , "f"  };
int i = Array.IndexOf(ar, "d" , 1);      //在abc数组中查找"d"所有的位置,从abc[1]开始找,结果:2
int l = Array.LastIndexOf(ar, "a" , 0);  //在abc数组中查找"c"所有的位置,从abc[0]开始找,结果:0
Array.Reverse(ar);                      //颠倒ar数组,此时的ar[0]等于"f"
Array.Sort(ar);                         //Sort与Reverse相反
object[] oar ={1, false ,1.5, "string" };   //定义一个可以接受任何类型的数组;
 
//Array型数组要重定义大小,对于大数组会特别慢;且无法在中间插入元素;不能清除它们(只能设置为空或0) ;
不能像javascript数组那样用push添加;
 
三、ArrayList 类 (System.Collections)
ArrayList需要引用:using System.Collections;
 
ArrayList就是动态数组,是Array的复杂版本,它提供了如下一些好处:
1.动态的增加和减少元素
2.实现了ICollection和IList接口
3.灵活的设置数组的大小
 
ArrayList alist = new  ArrayList();
alist.Add(1);
alist.Add(2);
 
ArrayList 类,常用属性
Count属性:获取 ArrayList 中实际包含的元素的个数。
如:
int c = alist.Count;    //c等于2
 
ArrayList 类,常用方法
Contains方法:确定某元素是否在 ArrayList 中
如:
bool bl = alist.Contains( "a" );  bl等于True   
 
Add方法:将对象添加到 ArrayList 的结尾处。
如:
alist.Add(3);
 
ToArray方法:将ArrayList 的元素复制到指定元素类型的新数组中。
如:
Int32[] iar = (Int32[])alist.ToArray( typeof (Int32));
object[] oar = alist.ToArray( typeof (object));
 
遍历 ArrayList
第一种遍历
foreach(object o in  al)
{
     //o
}
第二种遍历
for (int i=0;i<alist.Count;i++)
{
     //alist[i]
}
第三种遍历
IEnumerator ie = alist.GetEnumerator();
while  (ie.MoveNext())
{
     //ie.Current.ToString()
}
 
五、List 泛型类 (System.Collections.Generic)
表示可通过索引访问的对象的强类型列表
需要引用using System.Collections.Generic;
 
List<obj> list = new  List<obj>();
list.Add( new  obj(1 2, 1));
list.Add( new  obj(2, 4, 3));
 
Count属性:获取 List<T> 中实际包含的元素的个数。
如:
int c = list.Count;    //c等于2
 
Add方法:将对象添加到 List<T> 的结尾处。
如:
list.Add( new  obj(3, 4, 5));
 
Contains方法:确定某元素是否在 List<T> 中。
如:
bool bl = list.Contains( new  obj(2, 4, 3));  bl等于True
 
六、List类应用
通过 for 添加隐式类型
如:
List<object> list = new  List<object>();
for  (int i = 0; i < 5; i++)
{
     list.Add( new  { name = "sntetwt" , age = 100 });
}
 
dropdownlist控件绑定泛型list<T>
如:
DropDownList drp = new  DropDownList();
drp.DataSource = list;
drp.DataTextField = "name" ;
drp.DataValueField = "age" ;
drp.DataBind();<br><br>

//把数字字符串反序列化为ListList<int>集合;
//以下述方法只对[1,2,5,3]这样起作用,并且不能是"1,2,5,3"这种格式;
List<int> listid = new JavaScriptSerializer().Deserialize<List<int>>("[1,2,5,3]");
//格式:List<int> listid = new List<int> {1,2,5,3};
foreach (int id in listid) 
Response.Write(id); //结果:1253

<br>七、IEnumerable(遍历) 接口 (System.Collections)
<em id= "__mceDel" >公开枚举数,该枚举数支持在非泛型集合上进行简单迭代。
对集合使用foreach语句:
foreach(int i in  col){...}
 
相当于:
IEnumerator etor = ((IEnumerable)col).GetEnumerator();
try
{
   while (etor.MoveNext())
   {
    ElementType clem (ElementType)etor.Current;
    ...;
   }
}
finally{(IDisposable)enumerator).Dispose();}
 
实例应用:通过Linq查找再遍历,然后以JSON的格式输出到客户端;
using System.Linq;
using System.Web.Script.Serialization;
using System.Collections;
 
int[] items = new  int[] { 1, 2, 3, 4, 5 };
IEnumerable<int> ints = from item in  items
                         where item > 2.5
                         select item;
Response.Write( new  JavaScriptSerializer().Serialize(ints)); 结果:[3,4,5]
</em>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值