C#集合类型
2015-08-10 17:28
691 查看
1. 数组:
数组的初始化方式:
//数组初始化
int[] number1 = new int[5] { 1,2,3,4,5};
//或者
int[] number2 = new int[] { 1, 2, 3, 4, 5 };
//或者
int[] number3 = { 1, 2, 3, 4, 5 };
//多维数组初始化
string[,] name1 = { { "a", "k" }, { "h", "j" } };
//数组的数组
int[][] scores1 = { new int[]{ 1,2,3}, new int[]{ 3,4,5,6} };
//数组中成员的获取:
Console.WriteLine(number1[2]);
//数组中的方法:
Console.WriteLine(number1.Length);
//所有成员的获取:
foreach(int i in number1)
{
Console.WriteLine(i);
}
注意:数组计数从0开始
2. ArrayList 和 List
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//1. ArrayList : 继承于System.Collection,数组列表可以存储各种类型
ArrayList al = new ArrayList();
al.Add(5);
al.Add(100);
al.Remove(5);
al.Add("thystar");
foreach(var e in al)
{
Console.WriteLine(e);
}
//用数组列表访问成员
Console.WriteLine(al[0]);
//2. List : 只能存储一种类型
List<int> intList = new List<int>();
intList.Add(3);
intList.AddRange(new int[] { 5, 3, 2 });
//List中是否包含某个值:
Console.WriteLine(intList.Contains(3));
//List中某个数值的位置:
Console.WriteLine(intList.IndexOf(5));
//删除
intList.Remove(5);
//在中间某个位置插入
intList.Insert(1, 222);
Console.ReadLine();
}
}
}
3. Hashtable 和 Dictionary, 以及其他一些集合类型:
// 哈希表:key : value形式, 对类型没有强制规定
Hashtable ht = new Hashtable();
ht.Add("1", "first");
ht.Add("2", "second");
Console.WriteLine(ht["1"]);
//哈希表访问无效key值时返回空,不会报错
//用Dictionary替代哈希表,强制规定数据类型:
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("first", "hello");
d.Add("second", "world");
Console.WriteLine(d["first"]);
//Dictionary访问无效key时会报错;因此,它比哈希表更安全;
//但是,Dictionary不是一个线程安全的类型:
// 线程安全的类型用ConcurrentDictionary
// SortedList: 经过排序的List, 通过key的值排序
SortedList<int, int> sl = new SortedList<int, int>();
sl.Add(3, 4);
sl.Add(2, 5);
sl.Add(4, 6);
sl.Add(1, 100);
foreach(var sle in sl)
{
Console.WriteLine(sle);
Console.WriteLine(sle.Value);
}
// stack 栈类型:先进后出
// queue 队列: 先进先出
namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //数组;继承于抽象类System.Array int[] number = new int[5]; //二维数组: string[,] name = new string[5, 4]; //数组的数组,每一行长度可以不同 byte[][] scores = new byte[5][]; for (int i=0; i<scores.Length; i++) { scores[i] = new byte[i + 3]; } for (int i = 0; i < scores.Length; i++) { Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length); } Console.ReadLine(); } } }
数组的初始化方式:
//数组初始化
int[] number1 = new int[5] { 1,2,3,4,5};
//或者
int[] number2 = new int[] { 1, 2, 3, 4, 5 };
//或者
int[] number3 = { 1, 2, 3, 4, 5 };
//多维数组初始化
string[,] name1 = { { "a", "k" }, { "h", "j" } };
//数组的数组
int[][] scores1 = { new int[]{ 1,2,3}, new int[]{ 3,4,5,6} };
//数组中成员的获取:
Console.WriteLine(number1[2]);
//数组中的方法:
Console.WriteLine(number1.Length);
//所有成员的获取:
foreach(int i in number1)
{
Console.WriteLine(i);
}
注意:数组计数从0开始
2. ArrayList 和 List
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//1. ArrayList : 继承于System.Collection,数组列表可以存储各种类型
ArrayList al = new ArrayList();
al.Add(5);
al.Add(100);
al.Remove(5);
al.Add("thystar");
foreach(var e in al)
{
Console.WriteLine(e);
}
//用数组列表访问成员
Console.WriteLine(al[0]);
//2. List : 只能存储一种类型
List<int> intList = new List<int>();
intList.Add(3);
intList.AddRange(new int[] { 5, 3, 2 });
//List中是否包含某个值:
Console.WriteLine(intList.Contains(3));
//List中某个数值的位置:
Console.WriteLine(intList.IndexOf(5));
//删除
intList.Remove(5);
//在中间某个位置插入
intList.Insert(1, 222);
Console.ReadLine();
}
}
}
3. Hashtable 和 Dictionary, 以及其他一些集合类型:
// 哈希表:key : value形式, 对类型没有强制规定
Hashtable ht = new Hashtable();
ht.Add("1", "first");
ht.Add("2", "second");
Console.WriteLine(ht["1"]);
//哈希表访问无效key值时返回空,不会报错
//用Dictionary替代哈希表,强制规定数据类型:
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("first", "hello");
d.Add("second", "world");
Console.WriteLine(d["first"]);
//Dictionary访问无效key时会报错;因此,它比哈希表更安全;
//但是,Dictionary不是一个线程安全的类型:
// 线程安全的类型用ConcurrentDictionary
// SortedList: 经过排序的List, 通过key的值排序
SortedList<int, int> sl = new SortedList<int, int>();
sl.Add(3, 4);
sl.Add(2, 5);
sl.Add(4, 6);
sl.Add(1, 100);
foreach(var sle in sl)
{
Console.WriteLine(sle);
Console.WriteLine(sle.Value);
}
// stack 栈类型:先进后出
// queue 队列: 先进先出
相关文章推荐
- C#实现获取程序路径方法小结
- Visual C#2010学习笔记四之列表选择控件的使用
- C#反射(Reflection)的应用
- C#实现创建,删除,查找,配置虚拟目录实例详解
- C#一些实用的,容易被遗忘的特性,经验和技巧
- C#简单创建和删除目录的方法
- C# 关键字
- C# Word控件 文档保护
- C# 操作NPOI导入导出
- C#设置Cookies .
- C# Dictionary(字典)的键、值排序
- C# 注释(Comment)规范
- C#语言 -- 类型转换
- C#实现向已存在的Excel文件中写入数据实例(两种方法)
- C# 最小化窗口截图 句柄 调用截图 黑屏原因
- c# 委托
- 彻底理解C#中值类型和引用类型的区别
- WinForm容器内控件批量效验是否允许为空?设置是否只读?设置是否可用等方法分享
- WinForm容器内控件批量效验是否允许为空?设置是否只读?设置是否可用等方法分享
- C# 合并DLL, 合并DLL进入EXE 【转】