【 塔 · 第 一 条 约 定 】

整理c#:Array Arraylist List Hashtable Dictionary Stack Queue等

  1. Array 的容量是固定的,而 ArrayList 的容量是根据需要自动扩展的。ArrayList 提供添加、插入或移除某一范围元素的方法。
  2. ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处:

    动态的增加和减少元素

    实现了ICollection和IList接口

    灵活的设置数组的大小

    eg:(array)
  • 在声明数组的同时给其分配空间:

    数组类型[] 数组名=new 数组类型[数组长度]

    -声明数组,分配空间和赋值一同完成

    数组类型[]数组名={值1,值2,值3}

    例如:int[]arr={1,2,3}等价于int[]arr=new int[]{1,2,3}

    eg:(arraylist)
  • ArrayList arrl = new ArrayList() 个数不用指定
  • arrl.Add('')

     arrl.Remove('')

     arrl.RemoveAt('')

     arrl.RemoveRange(1, 3) 移除一到三号元素

     arrl.Sort() 升序

     arrl.Reverse() 反转

     arrl.Clear() 清空
  1. List

    eg:(List)
  • List mList = new List() 其中T为声明的种类

    T为列表中元素类型,现在以string类型作为例子:
List<string> mList = new List<string>();
  • List testList =new List (IEnumerable collection) 以一个集合为参数创建List
string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu"};

  List<string> testList = new List<string>(temArr);
  • List. Add(T item)添加一个元素
  • List. AddRange(IEnumerable collection)添加一组元素
string[] temArr = {"Ha","Hunter","Tom","Lily","Jay","Jim","Kuku","Locu"};mList.AddRange(temArr);
  • Insert(intindex, T item);在index位置添加一个元素
mList.Insert(1,"Hei");
  • 判断某个元素是否在该List中:

  List. Contains(T item)返回true或false

  

if(mList.Contains("Hunter"))

  {

  Console.WriteLine("There is Hunter in the list");

  }

  else

  {

  mList.Add("Hunter");

  Console.WriteLine("Add Hunter successfully.");

  }

(List与arraylist差不多)

4. 哈希表(Hashtable)

eg:

  • 命名空间:System.Collections
  • Hashtable hshTable = new Hashtable() 创建哈希表
  • hshTable .Add("Person1", "zhanghf") 往哈希表里添加键值对
  • hshTable .Clear() 移除哈希表里所有的键值对
  • hshTable .Contains("Person1") 判断哈希表里是否包含该键
  • string name = (string)hshTable["Person1"].ToString() 取哈希表里指定键的值
  • hshTable.Remove("Person1") 删除哈希表里指定键的键值对
  • IDictionaryEnumerator en = hshTable.GetEnumerator() 遍历哈希表所有的键,读出相应的值

    (也和那几个具体用法差不多)
  1. (Dictionary)
  • 结构:Dictionary<[key], [value]>
  • 他的特点是存入对象是需要与[key]值一一对应的存入该泛型

    通过某一个一定的[key]去找到对应的值

    eg:
//实例化对象
Dictionary<int, string> dic = new Dictionary<int, string>();
//对象打点添加
dic.Add(1, "one");
dic.Add(2, "two");
dic.Add(3, "one");
//提取元素的方法
string a = dic[1];
string b = dic[2];
string c = dic[3];
//1、2、3是键,分别对应“one”“two”“one”
//上面代码中分别把值赋给了a,b,c
  1. 堆栈(Stack)
  • 堆栈(Stack)代表了一个后进先出的对象集合。当您需要对各项进行后进先出的访问时,则使用堆栈。当您在列表中添加一项,称为推入元素,当您从列表中移除一项时,称为弹出元素。
  • Count 获取 Stack 中包含的元素个数。
  • public virtual void Clear();

    从 Stack 中移除所有的元素。
  • public virtual bool Contains( object obj );

    判断某个元素是否在 Stack 中。
  • public virtual object Peek();

    返回在 Stack 的顶部的对象,但不移除它。
  • public virtual object Pop();

    移除并返回在 Stack 的顶部的对象。
  • public virtual void Push( object obj );

    向 Stack 的顶部添加一个对象。
  • public virtual object[] ToArray();

    复制 Stack 到一个新的数组中。

    eg:
Stack st = new Stack();

            st.Push('A');
st.Push('M');
st.Push('G');
st.Push('W');
  1. 队列(Queue)
  • 队列(Queue)代表了一个先进先出的对象集合。当您需要对各项进行先进先出的访问时,则使用队列。当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队。

    (与堆栈差不多)

整理C#:for、foreach、while、switch等

  1. for

    和c差不多
  2. foreach:

    foreach 语句为数组或对象集合中的每个元素重复一个嵌入语句组。foreach 语句用于循环访问集合以获取所需信息,但不应用于更改集合内容以避免产生不可预知的副作用。
  • C#中:foreach 针对引用类型地址的访问

    如果当前方法中在访问当前引用类型的集合,在新调用的 方法中在修改当前集合的地址时,当前地址会依然存在,不符合C#中的对象的生命周期,一旦当前文件的引用地址被替换掉了,当前对象的空间就消失废弃了,但是,foreach(){

    }会保留原先的集合的地址<地址应该也是有生命周期的>foreach 和for 就不一样!
  • 语法格式如下:

foreach(type identifier in expression)

{

embedded-statement

}

type(类型)和identifier(标识符)用于声明循环变量,expression(表达式)对应集合。

 int[]arr=newint[]{0,1,2,3,4};
foreach(int i in arr)
{
Console.Write(i);
}
  1. while
int i=0;
  while(i<arr.Length)
  {
    Console.Write(arr[i] + " ");
    i++;
  }
  Console.WriteLine();
  1. switch

    (和c差不多)

    借了个栗子:
Console.Write("请输入分数(整数): ");
  int score_in = Convert.ToInt32(Console.ReadLine());
  if (score_in < 0) score_in = -100;//防止-9到-1被归为不及格
  switch(score_in/10)
  {
    case 10:
    case 9: Console.WriteLine("优秀"); break;
    case 8: Console.WriteLine("良好"); break;
    case 7:
    case 6: Console.WriteLine("及格"); break;
    case 5:
    case 4:
    case 3:
    case 2:
    case 1:
    case 0: Console.WriteLine("不及格"); break;
    default: Console.WriteLine("输入错误!"); break;
  }

有关c#的学习笔记整理与心得的更多相关文章

  1. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  2. Deep Learning(深度学习)学习笔记整理系列之(五)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  3. Deep Learning(深度学习)学习笔记整理系列之(八)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  4. Deep Learning(深度学习)学习笔记整理系列之(七)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  5. Deep Learning(深度学习)学习笔记整理系列之(六)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  6. Deep Learning(深度学习)学习笔记整理系列之(四)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  7. Deep Learning(深度学习)学习笔记整理系列之(三)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  8. Deep Learning(深度学习)学习笔记整理系列之(二)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  9. Deep Learning(深度学习)学习笔记整理系列之(一)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0  2013-0 ...

随机推荐

  1. JSON API免费接口 各种提供JSON格式数据返回服务网站的API接口

    这里为大家搜集了一些能够返回JSON格式的服务接口.部分需要用JSONP调用. 电商接口 京东获取单个商品价格接口: http://p.3.cn/prices/mgets?skuIds=J_商品ID& ...

  2. Cannot send session cache limiter - headers already sent问题

    在php.ini中将“always_populate_raw_post_data ”设置为“-1”,并重启

  3. Python在线编程环境

    除了安装Python的IDE之外,也可以使用在网页中随时随地编写Python程序. Python官网:https://www.python.org/shell Python123:https://py ...

  4. 树莓派安装samba

    (1) sudo apt-get install samba samba-common (2)mkdir /home/lin/share #(文件路径自己添加) (3)sudo chmod 777 / ...

  5. C#正则表达式提取HTML中IMG标签的SRC地址(转)

    一般来说一个 HTML 文档有很多标签,比如“<html>”.“<body>”.“<table>”等,想把文档中的 img 标签提取出来并不是一件容易的事.由于 i ...

  6. [ES]Elasticsearch在windows下的安装

    1.环境 win7 64位 2.下载包环境 https://www.elastic.co/cn/downloads/elasticsearch 选择对应安装包 3.安装过程 解压安装包,例如我的,解压 ...

  7. Unknown host 'services.gradle.org' 解决方法

    报错如下: Unknown host 'services.gradle.org'. You may need to adjust the proxy settings in Gradle. Learn ...

  8. 在Sqlserver中生成随机数据

    百度了各种随机生成,集中摘录如下: 一.循环写入千万级测试数据 DECLARE @i int ) BEGIN INSERT INTO A_User(username,password,addtime, ...

  9. 深入解析UUID及其应用(转载)

    UUID 编辑 UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Founda ...

  10. 关于 Windows 10 字体安装目录的问题

    不知从什么时候开始,本人台式机的Win10系统在安装字体的时候并不是安装到C:\Windows\Fonts目录中,而是安装到%USERPROFILE%\AppData\Local\Microsoft\ ...