C#集合类型
来源:互联网 发布:淘宝卖家后台功能介绍 编辑:程序博客网 时间:2024/05/27 20:50
1. 数组:
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 队列: 先进先出
0 0
- C# 集合类型
- C#的集合类型
- C#集合类型
- C# 集合类型
- c#常用的集合类型
- c#强类型化集合
- C#集合类型大盘点
- C#集合类型大盘点
- C# ArrayList 和 List 集合类型
- C#中SortedList 和 SortedDictionary集合类型
- 自定义C#值类型的的集合
- C# HashSet集合类型使用介绍
- C#集合类型总结和性能分析
- c#基础之集合 类型总结
- C#高级编程四十七天----集合接口和类型
- c#中常用的集合类型及基本用法
- C#获取泛型集合中元素的类型
- C# LINQ去重复杂类型对象集合
- java多线程(一)
- java.lang.IllegalStateException: Optional int parameter 'currentSpecialIndex' is present but cannot
- Java Web基础——自定义标签和JSTL
- 【姿势】Android 和 Mac 爆毁灭级安全漏洞
- enum Size
- C#集合类型
- iOS将汉字转换为拼音
- UITableVIew 滚动流畅性优化
- hdu1556 Color the ball(线段树区间更新)
- 112. Path Sum
- 通用券抢券的优化过程.
- android学习笔记(9)android程序调试学习
- BZOJ1006
- 树结构练习——判断给定森林中有多少棵树