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. Python内置数据类型之Dictionary篇

    1.查看函数XXX的doc string. Python的函数是有属性的,doc string便是函数的属性.所以查看函数XXX的属性的方法是模块名.XXX.__doc__ 2.模块的属性 每个模块都 ...

  2. nodejs开发阶段利器supervisor

    在开始学习nodejs时,往往一般写代码,一边看效果.先停止node,再重新运行.非常耗时. 这时supervisor派上了用场. 安装 推荐使用npm,本人一直使用局部安装,这样可以将全部文件安装在 ...

  3. MySQL内存表-临时表

    HEAP表是访问数据速度最快的MySQL表,他使用保存在内存中的散列索引.但如果MySQL或者服务器重新启动,表中数据将会丢失.用法:如论坛的在线人数统计,这种表的数据应该是无关紧要的,就几个简单的字 ...

  4. HDU 4608 I-number 2013 Multi-University Training Contest 1

    定义一个数 y 为 x 的 I-number.对于 y 有如下要求: 1.y > x; 2.y 的每一位之和要为10的倍数(例如 28 每一位之和为 10 ,为 10 的 1 倍); 3.这样的 ...

  5. iOS 复选框风格转换 Switchery 开关效果

    Switchery 是个简单的 JavaScript 组件,只要几个简单的步骤就可以帮助用户把默认的 HTML 复选框转换成漂亮 iOS 7 样式风格.用户可以很方便的自定义这种转换,所以可以很好的配 ...

  6. Java并发编程-总纲

    Java 原生支持并发,基本的底层同步包括:synchronized,用来标示一个方法(普通,静态)或者一个块需要同步执行(某一时刻,只允许一个线程在执行代码块).volatile,用来标识一个变量是 ...

  7. Delphi 关闭MDI子窗口

    需要在子窗口的onClose事件中吧Action = caFree; 就可以了. procedure Tfrm_aa.FormClose(Sender: TObject; var Action: TC ...

  8. mysql Access denied for user \'root\'@\'localhost\'”解决办法总结,下面我们对常见的出现的一些错误代码进行分析并给出解决办法,有需要的朋友可参考一下。

    mysql Access denied for user \'root\'@\'localhost\'”解决办法总结,下面我们对常见的出现的一些错误代码进行分析并给出解决办法,有需要的朋友可参考一下. ...

  9. 【leetcode】155 - Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  10. Python 时间整理

    在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: ...