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. xp关不了机

    自己测试:按第2点的设置既可成功1.点“开始→设置→控制面板→电源选项→高级电源管理”,如果你的机器支持高级电源管理功能,则选中“启用高级电源管理支持”:2.单击“开始”,并运行“regedit”,然 ...

  2. Spring 事务配置5种方式

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...

  3. 手机GUI自动化测试介绍

    手机GUI自动化测试介绍 Posted on 2013/05/15 Xing Binbin(测试工程师) 摘要 众所周知,自动化测试可以一定程度上减轻测试人员负担,提高测试效率,并且通过自动化还可以实 ...

  4. 【转】linux设备驱动程序中的阻塞机制

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275272.html 阻塞与非阻塞是设备访问的两种方式.在写阻塞与非阻塞的驱动程序时,经 ...

  5. android4.0以上部分手机绘图时会出现重影

    canvas外层的div需要设定属性style="overflow:visible;-webkit-transform: translateZ(0);

  6. 部署K2 Blackpearl流程时出错(与基础事务管理器的通信失败或Communication with the underlying transaction manager has failed.

    转:http://www.cnblogs.com/dannyli/archive/2011/12/01/2270222.html 亲,在部署K2流程是,是否遇到这个错误(以下是中.英文错误信息) 中文 ...

  7. 对于REST中无状态(stateless)的一点认识

    今天早上在Yahoo的邮件列表里看到一篇颇有意思的讨论,标题为RESTful vs. unRESTful: Session IDs and Authentication(51CTO编者注:意为REST ...

  8. 构建 XCache 的基本步骤

    构建 XCache 的基本步骤 在开始之前,首先确保 PHP 正常安装并核实 phpize 是否位于 shell 的 PATH 下.同时,还需要一个 C 编译器,例如 GNU Compiler Col ...

  9. 挺好看的CSS

    <?php header("content-type:text/html;charset=utf-8"); echo "<style>h1{ color ...

  10. PubSub的一种实现

    今天在浏览JavaScript事件时,复习了下Dean Edward大神的addEvent.突然觉得可以基于他的思路实现一个结构更好的PubSub. 思路也很简单,就是要维护一个类似如下的一个仓库结构 ...