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练习程序(c100经典例9)

    题目: 要求输出国际象棋棋盘. for i in range(1,9): for j in range(1,9): if i%2==0: if j%2==0: print '*', else: pri ...

  2. *ecshop安装模板

    1. 安装模板 例: 新建abc.com文件 复制default下的style.css 修改两个http://www.ecshop.com/ 为 http://www.hua.com/ 复制defau ...

  3. 支持向量机之Hinge Loss 解释

    Hinge Loss 解释 SVM 求解使通过建立二次规划原始问题,引入拉格朗日乘子法,然后转换成对偶的形式去求解,这是一种理论非常充实的解法.这里换一种角度来思考,在机器学习领域,一般的做法是经验风 ...

  4. POJ2236 Wireless Network

    解题思路:简单并查集,注意时间限制是10000MS,每次进行O操作之后,   进行一次for循环,进行相关调整.同时注意输入输出格式,见代码: #include<cstdio> #incl ...

  5. 微信 ua

    Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, l ...

  6. Android 开源项目PhotoView源码分析

    https://github.com/chrisbanes/PhotoView/tree/master/library 这个就是项目地址,相信很多人都用过,我依然不去讲怎么使用.只讲他的原理和具体实现 ...

  7. selenium python (八)定位frame中的对象

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip'#在测试过程中经常遇到frame嵌套的应用,加入页面上有A.B两个fram ...

  8. PHP.ini 配置文件解析

    [PHP] ;;;;;;;;;;;;;;;;;;;; About php.ini   ;;;;;;;;;;;;;;;;;;;;; PHP's initialization file, generall ...

  9. Android中设置全屏的方法

    在实际的应用程序开发中,我们有时需要把 Activity 设置成全屏显示,一般情况下,可以通过两种方式来设置全屏显示效果.其一,通过在代码中可以设置,其二,通过manifest配置文件来设置全屏. 其 ...

  10. 千万别把js的正则表达式方法和字符串方法搞混淆了

    我们在字符串操作过程中肯定经常用了test() split() replace() match() indexof()等方法,很多人经常把用法写错了,包括我,所以今天细细的整理了下. test()是判 ...