一、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();

//把数字字符串反序列化为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

七、IEnumerable(遍历) 接口 (System.Collections)
公开枚举数,该枚举数支持在非泛型集合上进行简单迭代。
对集合使用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]

  

Array、ArrayList、List、IEnumerable、for、foreach应用的更多相关文章

  1. Array,ArrayList,泛型List比较

    在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...

  2. c#中Array,ArrayList 与List<T>的区别、共性与转换

    本文内容来自我写的开源电子书<WoW C#>,现在正在编写中,可以去WOW-Csharp/学习路径总结.md at master · sogeisetsu/WOW-Csharp (gith ...

  3. 类 Array Arraylist List Hashtable Dictionary

    总结C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue  我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashT ...

  4. 一点一点看JDK源码(五)java.util.ArrayList 后篇之forEach

    一点一点看JDK源码(五)java.util.ArrayList 后篇之forEach liuyuhang原创,未经允许禁止转载 本文举例使用的是JDK8的API 目录:一点一点看JDK源码(〇) 代 ...

  5. 扩展IEnumerable<T> ForEach()方法

      相信很多人,在用Linq时,都会困惑为什么IEnumerabel<T>没有ForEach,虽然 我们一样可以这样写,很快读写 foreach(item in items) { Cons ...

  6. C# 集合类 Array,Arraylist,List,Hashtable,Dictionary...

    我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类.我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和 ...

  7. c# array arraylist 泛型list

    1 array 数组  是存储相同类型元素的固定大小的数据的顺序集合.在内存中是连续存储的,所以索引速度非常快,而且赋值和修改元素也非常简单. //定义字符串数组 大小为3 string[] str1 ...

  8. C#中 [], List, Array, ArrayList 區別

    [] 是針對特定類型.固定長度的.List 是針對特定類型.任意長度的.Array 是針對任意類型.固定長度的.ArrayList 是針對任意類型.任意長度的.Array 和 ArrayList 是通 ...

  9. IEnumerable 使用foreach 详解

    自己实现迭代器 yield的使用 怎样高性能的随机取IEnumerable中的值 我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么L ...

  10. Array,ArrayList、List<T>、HashSet<T>、LinkedList<T>与Dictionary<K,V>

    Array: 数组在C#中最早出现的.在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单. 但是数组存在一些不足的地方.在数组的两个数据间插入数据是很麻烦的,而且在声明数组的时候 ...

随机推荐

  1. Excel部署配置DCOM

    对 Excel进行编程,实际上就是通过 .Net Framework去调用 Excel的 COM组件,所有要在 Web环境下调用 COM组件的时候,都需要对其进行相应的配置. 很多朋友都反映在 Win ...

  2. centos7虚拟机(vmware)通过U盘传文件

    centos7虚拟机(vmware)通过U盘传文件 centos7虚拟机安装以后,WINDOWS给CENTOS7传文件,除了在CENTOS7安装SAMBA外,其实通过U盘也是可以的. CENTOS7对 ...

  3. c++ strcmp strcpy sprintf

  4. 自定义兼容多种Protobuf协议的编解码器

    <从零开始搭建游戏服务器>自定义兼容多种Protobuf协议的编解码器 直接在protobuf序列化数据的前面,加上一个自定义的协议头,协议头里包含序列数据的长度和对应的数据类型,在数据解 ...

  5. HikariCP 脑火Failed to obtain JDBC Connection: You need to run the CLI build and you need target/classes in your classpath to run.

    测试了一下 HikariCP 连接池报错,无解 十一月 16, 2017 5:31:59 下午 org.apache.catalina.core.StandardContext loadOnStart ...

  6. 玩转Eclipse — 自动代码生成的Java Code Template

    文章转载地址:点击打开链接 当代码写到一定程度之后,就会发现很多代码都被重复地敲了N多遍,甚至毫不夸张地说:闭着眼睛都能敲出来.大量地敲这些重复地代码,除了锻炼敲键盘的速度,基本上没有其他益处,但是长 ...

  7. rocketmq 管理控制台

    docker pull styletang/rocketmq-console-ng    docker run -e "JAVA_OPTS=-Drocketmq.namesrv.addr=1 ...

  8. [Android Security] 如何把java代码转换成smali代码

    copy :https://www.cnblogs.com/gordon0918/p/5466514.html 1.概述 Smali是Android系统中Dalvik虚拟机指令语言,在apk逆向过程中 ...

  9. Asp.Net Core WebAPI入门整理(四)参数获取

    一.总结整理,本实例对应.Net Core 2.0版本 1.在.Net Core WebAPI 中对于参数的获取及自动赋值,沿用了Asp.Net  MVC的有点,既可以单个指定多个参数,右可以指定Mo ...

  10. sulime代理设置、插件管理

    使用command palette或者package control,可以管理插件:安装.更新.启动.关闭插件.卸载插件等 配置Package Control 配置举例: { "bootst ...