C#数组和集合整理
写起来还是有些勉强的,还有很多用法没有完全理解,只整理了一些基本点。
Array
也就是数组。
具体表示方法是:数据类型[维数] 数组名=new 数据类型[]
举例如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[3];
int[] b = new int[3] { 1, 2, 3 };
int[] c = new int[] { 1, 2, 3 };
int[,] d = new int[3,3];
}
}
}
ArrayList
动态数组,用法似乎跟c++的vector有点像。使用ArrayList必须引用Collections类。
声明 ArrayList a=new ArrayList();
添加
- Add(a) 添加元素a到末尾;
- Insert(b,a) 在位置b插入元素a;
- InsertRange(b,a) 在位置b插入集合a;
删除
- Remove(a) 移除元素a;
- RemoveAt(a) 移除位置a的元素;
- RemoveRange(a,b) 移除位置a到位置b的元素;
- Clear() 清空;
排序 Sort();
反转 Reverse();
查找
- IndexOf(a) 返回元素a的位置,没有则返回-1;
- Contains(a) 检测是否含有元素a,返回true/false;
输出元素
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList a = new ArrayList();
foreach (int i in a) Console.WriteLine(i); //不需要强制转换
for (int i = 0; i < a.Count; i++) //与数组的Length不同
{
int n = (int)al2[i]; //需要强制转换
Console.WriteLine(n);
}
}
}
}
List
List类是ArrayList类的泛型等效类,它的大部分用法都与ArrayList相似。最大的区别就是在声明List集合时,我们需要同时声明List内元素的数据类型。
不过,大部分情况下,List似乎比ArrayList更加安全和高效,原因在于ArrayList会把所有插入其中的数据作为object类型来处理,所以在用ArrayList处理数据时,很可能会出现类型不匹配的错误,并且装箱和拆箱的过程会带来很大的性能耗损。关于这一点还不是很理解,会继续学习。
声明方式:
List<int> a = new List<int>();
Hashtable
传说中的哈希表。
哈希表的内部是无序散列,也就是说,其输出不是按照开始加入的顺序,但这也保证了高效率。如果以任意类型键值访问其中元素会快于其他集合,特别是当数据量特别大的时候,效率差别尤其大。如果一定要排序HashTable输出,只能自己实现。
声明:Hashtable a = new Hashtable();
Add(a,b) 在哈希表中添加键值对;
Clear() 清除哈希表中的键值对;
Contains(a) 判断哈希表中是否含有键a;
Remove(a) 删除哈希表中的键值对a;
ToString(); 返回当前Object的string;
示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Hashtable a = new Hashtable();
a.Add(1, 1); //添加键值对
a[1] = 2; //给指定键赋值
//遍历1
foreach (DictionaryEntry b in a)
{
Console.WriteLine("{0}{1}", b.Key, b.Value);
}
//遍历2
IDictionaryEnumerator c = a.GetEnumerator();
while (c.MoveNext())
{
Console.WriteLine("{0}{1}", c.Entry.Key, c.Entry.Value);
}
//按序输出
ArrayList d = new ArrayList(a.Keys);
d.Sort();
foreach (int e in d)
{
Console.WriteLine(a[e]);
}
}
}
}
Dictionary
Dictionary与Hashtable类似,但是Dictionary遍历的顺序就是加入的顺序。
声明:Dictionary<string, string> a = Dictionary<string, string>();
Add(a,b) 在字典中添加键值对;
Clear() 清除字典中的键值对;
Contains<a,b> 判断字典中是否含有键值对;
ContainsKey(a) 判断字典中是否含有键a;
ContainsValue(a) 判断字典中是否含有值a;
Remove(a) 删除哈希表中的键值对a;
示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> a = new Dictionary<string, string>();
a.Add("a", "aa"); //添加键值对
a.Add("b", "bb");
a["a"] = "cc"; //给指定键赋值
//用泛型结构体遍历
foreach (KeyValuePair<string, string> b in a)
{
Console.WriteLine("{0}{1}", b.Key, b.Value);
}
//获得值集合
foreach (string c in a.Values)
{
Console.WriteLine(c);
}
}
}
}
Stack
后进先出。
声明:Stack a = new Stack();
Pop() 出栈;
Push(a) 进栈;
Count 获得栈包含的元素数;
Peek() 获得栈顶元素;
Contain(a) 判断栈中是否含有元素a;
Clear() 清除栈;
ToArray() 将栈复制到数组;
示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Stack a = new Stack();
a.Push(1); //入栈
a.Push(2);
a.Pop(); //出栈
Console.WriteLine("{0}{1}",a.Count,a.Peek()); //输出栈的元素个数
//遍历
foreach (int b in a)
{
Console.WriteLine(b);
}
}
}
}
Queue
先进先出。
声明:Queue a = new Queue();
Enqueue(a) 入队;
Dequeue() 出队;
Count 获得队列包含的元素数;
Peek() 获得队列最前端元素;
Contain(a) 判断队列中是否含有元素a;
Clear() 清除队列;
ToArray() 将队列复制到数组;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Queue a = new Queue();
a.Enqueue(1); //入队
a.Enqueue(2);
a.Dequeue(); //出队
Console.WriteLine("{0}{1}", a.Count, a.Peek()); //输出队列的元素个数
//遍历
foreach (int b in a)
{
Console.WriteLine(b);
}
}
}
}
C#数组和集合整理的更多相关文章
- 初学者入门web前端 C#基础知识:数组与集合
对于初学者,想要入门web前端,要有足够的信念和坚持,不然只会越走越远,我现在就深深的体会到. 我本是一个很拒绝代码的人,以前想过UI设计,但是在这段学习时间里,发现其实只要认真,代码并不是很难 所以 ...
- .NET 基础 一步步 一幕幕[数组、集合、异常捕获]
数组.集合.异常捕获 数组: 一次性存储多个相同类型的变量. 一维数组: 语法: 数组类型[] 数组名=new 数组类型[数组长度]; 声明数组的语法: A.数据类型 [] 数组名称= new 数据类 ...
- [.net 面向对象编程基础] (17) 数组与集合
[.net 面向对象编程基础] (17) 数组与集合 学习了前面的C#三大特性,及接口,抽象类这些相对抽象的东西以后,是不是有点很累的感觉.具体的东西总是容易理解,因此我们在介绍前面抽象概念的时候,总 ...
- paip.数组以及集合的操作uapi java php python总结..
paip.数组以及集合的操作uapi 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...
- c#重点[集合类型]异常,数组,集合ArrayList,List<>,hashTable,hashtable泛型(Dictionary)
1.foreach[对一些数组或集合进行遍历] foreach(类型 变量名 in 集合对象){语句体} //定义一个数组 ,,,,, }; foreach(var i in sNum1) { Con ...
- 创建泛类集合List以及数组转集合,集合转数组的应用
List<int> list = new List<int>(); list.Add(); list.Add(); list.Add(); list.AddRange(, , ...
- Java RGB数组图像合成 ImageCombining (整理)
/** * Java RGB数组图像合成 ImageCombinning (整理) * * 2016-1-2 深圳 南山平山村 曾剑锋 * * 注意事项: * 1.本程序为java程序,同时感谢您花费 ...
- Java比较器对数组,集合排序一
数组排序非常简单,有前辈们的各种排序算法,再加上Java中强大的数组辅助类Arrays与集合辅助类Collections,使得排序变得非常简单,如果说结合比较器Comparator接口和Collato ...
- java数组或集合返回空的问题
在有返回值的情况下,平时我写代码基本遇到错误什么都是返回null,我因为我觉得把数组或集合这个初始化占空间. 但是我发现这样在每次客户段调用都要进行非空判断,而且有时调用内置api还容易报错误,于是解 ...
随机推荐
- Mybait缓存机制
MyBatis同大多数ORM框架一样,提供了一级缓存和二级缓存的支持. 一级缓存:其作用域为session范围内,当session执行flush或close方法后,一级缓存会被清空. 二级缓存:二级缓 ...
- 理解call及apply
转载自:http://www.zhihu.com/question/20289071 //call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改 ...
- LeetCode(125):验证回文串
Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...
- Python计算器实操
要求: 开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * ...
- 抓包工具Charles Proxy v4.1.1破解版下载
移动开发抓包工具Charles Proxy破解版下载 下载Charles Proxy版本,http://charles.iiilab.com/或 https://www.charlesproxy.co ...
- C++ Primer 笔记——转发
某些函数需要将其一个或多个实参连同类型不变的转发给其他函数,这种情况下我们需要保持被转发实参的所有性质,包括实参类型是否是const的以及实参是左值还是右值. template <typenam ...
- NPOI操作Excel(一)--NPOI基础
用C#读取Excel的方法有很多中,由于近期工作需要,需要解析的Excel含有合并单元格以及背景色等特殊要求,故在网上查了一些关于读Excel的方法的优缺点,觉得NPOI能满足我的需要,所以搜索了一些 ...
- 论文阅读笔记三十三:Feature Pyramid Networks for Object Detection(FPN CVPR 2017)
论文源址:https://arxiv.org/abs/1612.03144 代码:https://github.com/jwyang/fpn.pytorch 摘要 特征金字塔是用于不同尺寸目标检测中的 ...
- 步步为营101-同一个PCode下重复的OrderNumber重新排序
USE [K2_WorkFlow_Test] GO /****** Object: StoredProcedure [dbo].[sp_UpdateBPM_DictionaryForOrderNumb ...
- Swap file ".hive-site.xml.swp" already exists
1.使用命令[hadoop@slaver1 conf]$ ls -la查找出隐藏文件,然后删除报出来的错误文件. [hadoop@slaver1 conf]$ rm -rf .hive-site.xm ...