C#中的Collection 1
Collection定义
Collection是个关于一些变量的集合,按功能可分为Lists,Dictionaries,Sets三个大类。
- Lists:是一个有序的集合,支持index look up。
- Array
- List<T>
- Dictionaries:同TKey store 一些TValue集合,没有特定order
- Sets:用来form collections into onther collection,比如HashSet<T>
Collection提供的一些公共方法

是Collection就至少会有Enumerat,但是不一定都有Look up(比如Sets就没有,Statcks和Queues也是有自己的固定排列顺序,不提供look up的方法),注意List的look up用index,而Dictionary的look up用Key。
For adding和removing的位置可能也不一样,list可以指定加和减的位置,而Statck是先进先出
Array
1: 有Fixed size并且是固定从上到下的顺序储存,index从0开始。
static void Main(string[] args)
{
//Declare
string[] daysOfWeek ={
"Monday",
"Tuesday",
"Wednesday",
"Thurday",
"Friday",
"Saturday",
"Sunday"
};
Console.WriteLine("Type in index of day of look up>");
int index = int.Parse(Console.ReadLine()); //look up using index
Console.WriteLine(daysOfWeek[index]); //Enumerates
foreach (string day in daysOfWeek)
{
Console.WriteLine(day);
} //replace
daysOfWeek[] = "PartyDay";
}
- 由于Array是固定size,所以没有Add和Remove item

2: Array是reference type,有两种initialize
int[] x4 = new int[]; //这个5不可以省
int[] x5 = new int[] {, , , , }; //可以全部写出来
int[] x5 = new int[] {1, 4, 9, 16, 25}; //让compiler来认有几个元素
var x5 = {1, 4, 9, 16, 25}; //int[]可以换成var,后面赋值都可以都不写int[]



int eight = ;
int[] square = new int[]{
,
* ,
eight + ,
int.Parse(""),
(int)Math.Sqrt()
};
3: Enumerating
这里出现错误,foreach 的interation plceholder是只读的reference type,for不是

比较上面array[i]会改变原array里的元素,下面则不会

4: For VS Foreach
For:Can replace an array element,must directly access it using the index not using foreach
Foreach: Can't replace element(只读) but can modify value of elements,对其他readonly collection也一样
class Program
{
static void Main(string[] args)
{
Person[] student = new Person[]
{
new Person{Name = "Shawn", Age = },
new Person{Name = "Yawei", Age = }
};
//foreach的iteration placeholder是只读不可以改
//我们这里没有直接改这个reference
//指定的每个person object,比如指向别的object
//而是修改这个object里面的值
foreach (Person person in student)
{
person.Age = ;
} foreach (Person person in student)
{
Console.WriteLine(person);
}
Console.ReadLine();
} }
public class Person
{
public string Name{get; set;}
public int Age{get; set;}
public override string ToString()
{
return string.Format("{0} 's age = {1}", Name, Age);
}
}
在collection里包括array == 是判断reference是否相等的,而不是值。
Derived reference can used in place of base reference(this can apply to all collections)
static void Main(string[] args)
{
Human[] human = new Human[]
{
new Human{Name = "Shawn", Age = },
new Human{Name = "Yawei", Age = }
};
human[] = new Employee { Name = "Tutu", Age=, Salary=120.2f};
}
public class Employee : Human
{
public float Salary { get; set; }
}
public class Human{
public string Name { get; set; }
public int Age { get; set; }
}
5: Convariance

static void Main(string[] args)
{
object[] obj1 = new object[];
string[] daysOfWeeks = {
"Monday",
"Tuesday",
"Wendesday"
};
//this can work
obj1[] = ; //这个是reference type,obj2 point to daysOfWeeks's location
object[] obj2 = daysOfWeeks;
//这句有问题,因为refer的地址是一个string[] type
//compile time的时候VS发现不了问题,会直接给runtime 带来break的后果
//same result as daysOfWeeks[0] = 3;
obj2[] = ;
daysOfWeeks[] = ;
}
解释:
一般Convariance不允许用在Collection(List<T>, Dictionial<TKey, TValue>)上即Collection of derived type to a collection of base type,即使像上面用在了array上也会有或许被broke掉的危险。
但是enumerator,enumerable可以做convariance,因为他们是只读的type保证是安全的,不会修改collection。
var strList = new List<string>{"Monday","Tuesday"};
List<object> objList = (List<Object>)strList;
//compiler will show error, can not do this kind of covariance
IEnumerable<object> objEnum = strList;
//this can do
6: Array的方法
一般有普通Array方法或者LINQ方法,Array方法tend to work in-place,而LINQ总是tend to return new object
Copy:
- Array.CopyTo(普通)
- ToArray(LINQ)
Reverse:
- Array.Reverse(普通,static方法)
- var reversed = daysOfWeek.Reverse(); (LINQ方法)
注意LINQ方法经常return IEnumerable 而不是Array,如果需要变为Array要在后面加一个.ToArray

Sort:
Array.Sort
Array.Sort([T], comparer)
IEnumerable<T>.OrderBy();
Binary Search:
Array.BinarySearch([T], "Sunday")
Search
Find item in array: Array.IndexOf(), Array.LastIndexOf()
Find item with condition in array: Array.FindIndex([T], x=> x[0] == 'W'), Array.FindAll([T], x=> x.length == 6)
C#中的Collection 1的更多相关文章
- Mybatis中的collection、association来处理结果映射
前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:) 标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...
- 优雅使用 illuminate/database 包中的 Collection
优雅使用 illuminate/database 包中的 Collection 或许你很抵抗使用 Laravel , 但是你没有理由不喜欢使用 illuminate/database.这是一个 ORM ...
- java中集合Collection转list对象
参考:java中集合Collection转list对象 首先我的需求是获取到购物车列表,购物车列表是一个Map对象,构造方法获取购物项,这里购物项是Collection对象 // 购物项集合,K商品I ...
- Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版)
Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版) XML版本: 实体类: @Data @NoArgsConstructor public class Course ...
- ruby -- 进阶学习(十)自定义路由中:new, :collection和:member的区别
学习链接:http://rubyer.me/blog/583/ RESTful风格的路由动词默认有7个(分别为:index, show, create, new, edit, update, dest ...
- java中的Collection集合类
随着1998年JDK 1.2的发布,同时新增了常用的Collections集合类,包含了Collection和Map接口.而Dictionary类是在1996年JDK 1.0发布时就已经有了.它们都可 ...
- mybatis 中 foreach collection的三种用法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...
- mybatis 中 foreach collection的三种用法(转)
文章转自 https://blog.csdn.net/qq_24084925/article/details/53790287 oreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集 ...
- java 中的Collection
/* *一. Collection?-------->容器! * * 1.来源于java.util包 非常实用的数据结构! * *二. 方法? * * void clear()删除集合中所有元素 ...
- JAVA 中的 Collection 和 Map 以及相关派生类的概念
JAVA中Collection接口和Map接口的主要实现类 Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的 ...
随机推荐
- 虚拟机下Linux系统安装vmtool工具
1.启动此虚拟机2.用超级用户root登录3.登录成功后,Ctrl+Alt ,取出鼠标,点选菜单栏,虚拟机 →安装VMware工具 → Install4.待虚拟系统挂载了光盘后,弹出cdrom文件夹或 ...
- php 获取指定日期所在月份的最后一天
本文引用来自 http://hi.baidu.com/yflife/item/fd00ef142c5967fcdceeca84 php 获取指定月最后一天: <?phpfunction gett ...
- Android下实现win8的按钮点击效果
原理就是自定义一个imageButton,实现动画效果 demo源码下载地址: 请戳这里----------------> 关于回弹张力的效果扩展,可以参考Facebook的开源动画库rebo ...
- PV是什么意思
现在国内的站点都号称自己能统计pv,不过听我讲完pv到底是什么,估计所有的站长都要笑了. pv的意思,就是一个访问者在24小时(0点到24点)内到底看了你网站几个页面. 不过在国内,由 ...
- js实现密码加密
http://www.cnblogs.com/mofish/archive/2012/02/25/2367858.html 1.base64加密 在页面中引入base64.js文件,调用方法为: &l ...
- RedHat 5 配置CentOS yum 更新源
YUM是Redhat Linux在线安装更新及软件的工具,但是这是RHEL5的收费功能,如果没有购买Redhat的服务时不能使用RHEL5的更新源的,会提示注册. 由于CentOS是从Redhat演化 ...
- 指定URL,计算文件大小
将http://www.baidu.com替换成指定的URL,就可以获得文件的大小. 文件大小已经转换为以KB为单位. url对象用openconnection()打开连接:获得URLConnecti ...
- Epic - Coin Change
Something cost $10.25 and the customer pays with a $20 bill, the program will print out the most eff ...
- Linux 权限相关
Linux中,所有文件都有 三种权限:User ,Group,Other 三个文件: /etc/passwd :包括所有系统账号,一般用户身份和root信息 /etc/shadow :保存个人密码 / ...
- Scrum之Sprint物件
产品订单(Product Backlog) 一个需求的列表. 一般情况使用用户故事来表示backlog条目 理想情况每个需求项都对产品的客户或用户有价值 Backlog条目按照商业价值排列优先级 优先 ...