Collection定义

Collection是个关于一些变量的集合,按功能可分为Lists,Dictionaries,Sets三个大类。

  1. Lists:是一个有序的集合,支持index look up。

    • Array
    • List<T>
  2. Dictionaries:同TKey store 一些TValue集合,没有特定order
  3. 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的更多相关文章

  1. Mybatis中的collection、association来处理结果映射

    前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:)   标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...

  2. 优雅使用 illuminate/database 包中的 Collection

    优雅使用 illuminate/database 包中的 Collection 或许你很抵抗使用 Laravel , 但是你没有理由不喜欢使用 illuminate/database.这是一个 ORM ...

  3. java中集合Collection转list对象

    参考:java中集合Collection转list对象 首先我的需求是获取到购物车列表,购物车列表是一个Map对象,构造方法获取购物项,这里购物项是Collection对象 // 购物项集合,K商品I ...

  4. Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版)

    Mybatis中使用collection进行多对多双向关联示例(含XML版与注解版) XML版本: 实体类: @Data @NoArgsConstructor public class Course ...

  5. ruby -- 进阶学习(十)自定义路由中:new, :collection和:member的区别

    学习链接:http://rubyer.me/blog/583/ RESTful风格的路由动词默认有7个(分别为:index, show, create, new, edit, update, dest ...

  6. java中的Collection集合类

    随着1998年JDK 1.2的发布,同时新增了常用的Collections集合类,包含了Collection和Map接口.而Dictionary类是在1996年JDK 1.0发布时就已经有了.它们都可 ...

  7. mybatis 中 foreach collection的三种用法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...

  8. mybatis 中 foreach collection的三种用法(转)

    文章转自 https://blog.csdn.net/qq_24084925/article/details/53790287 oreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集 ...

  9. java 中的Collection

    /* *一. Collection?-------->容器! * * 1.来源于java.util包 非常实用的数据结构! * *二. 方法? * * void clear()删除集合中所有元素 ...

  10. JAVA 中的 Collection 和 Map 以及相关派生类的概念

    JAVA中Collection接口和Map接口的主要实现类   Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的 ...

随机推荐

  1. 【英语】Bingo口语笔记(20) - i长短音

    短音有个ei的音,多练习下 长音是咦拉长

  2. C# 对Excel 单元格格式, 及行高、 列宽、 单元格边框线、 冻结设置

    一.对行高,列宽.单元格边框等的设置 这篇简短的文字对单元格的操作总结的比较全面,特此转载过来. private _Workbook _workBook = null; private Workshe ...

  3. n个数的最小公倍数

    Description 求n个数的最小公倍数.   Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数.   Output 为每组测试数据输出它们的最小公倍数,每个测 ...

  4. javascript针对DOM的应用

    所谓针对DOM的应用.也就我这里只教大家用javascript操作页面中dom元素做交互.我相信可能大部分人来这里学javascript主要还是想用这个结合页面中的DOM元素做一些实际有用的交互效果. ...

  5. 图Graph

    存储结构: 1.邻接矩阵存储 typedef struct { char vex[MAXVEX];//顶点数 int arc[MAXVEX][MAXVEX];//邻接矩阵 int numVextexe ...

  6. Oracle 热备份batch脚本 Windows

    1.最初来源于网络. 2.根据环境和喜好自己修改. 3.实测是可以完成备份任务的. 4.不推荐用于实际环境. bak.bat:执行时执行此脚本,其他脚本是调用和生成或者生成之后再调用.(需要自己修改先 ...

  7. DouNet学习_收发邮件

    一.收发邮件 --->第一步:发邮件首先要有发送者的邮箱地址和登录的密码才能发送 这些都写在APP里  不要写死 --->第二步:发邮件就要有网络,要添加net.Mail命名空间 要发送的 ...

  8. 转-问自己:UI设计注意的十个问题

    UI 设计需要自问的 10个问题   UI 设计的魅力在于,你不仅需要适当的技巧,更要理解用户与程序的关系.一个有效的用户界面关注的是用户目标的实现,包括视觉元素与功能操作在内的所有东西都需要完整一致 ...

  9. iframe和frame的区别

    在同时有frame和Iframe的一个窗口里frame最大可以做个frameset的儿子,Iframe最大也只能做到frameset的孙子.frame的布局限于几种,Iframe想放哪里放哪里.fra ...

  10. EventSource (node.js 与 OC)

    node.js服务器代码: var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, ...