c#点点滴滴之数据结构类
[1]Dictionary
Dictionary 表示键和值的集合 Dictionary(TKey, TValue) 类
公共属性
Comparer 获取用于确定字典中的键是否相等的 IEqualityComparer。
Count 获取包含在 Dictionary 中的键/值对的数目。
Item 获取或设置与指定的键相关联的值。
Keys 获取包含 Dictionary 中的键的集合。
Values 获取包含 Dictionary 中的值的集合。
1
using System;2
using System.Collections;3
using System.Collections.Generic; // Dictionary4
namespace ConsoleEnum5


{6
class testDictionary7

{8
static void Main()9

{10
Dictionary<string, int> dir = new Dictionary<string, int>(100);11
for (int i = 0; i < 100; i++)12

{13
dir.Add(i.ToString(), i);14
}15
foreach (string dirs in dir.Keys)16

{17
Console.WriteLine(dirs);18
}19
Hashtable ht = new Hashtable(100);20
for (int i = 0; i < 100; i++)21

{22
ht.Add(i.ToString(), i);23
}24
foreach (string dirs in ht.Keys)25

{26
Console.WriteLine(dirs);27
}28
}29
}30

31
}32

/**///////////////////////////////////////////////////////////////////33
using System;34
using System.Collections.Generic;35
class Dic36


{37
public static void Main()38

{39
Dictionary<int, int> dic_int = new Dictionary<int,int>();40
for (int i = 0; i < 10; i++)41

{42
//if ( i%2 == 0)43
// ++i; // 都是奇数键44
dic_int.Add(i, i + 10);45
}46

47
int value;48
for (int i = 0; i < dic_int.Count; i++)49

{50
value = dic_int[i]; // 所谓的下标访问,并不是按索引访问的。这里的下标,实际上是key,结果是value51
Console.WriteLine("{0}," , value );52
}53
}54
}55

/**/////////////////////////////////////56
10,57
11,58
12,59
13,60
14,61
15,62
16,63
17,64
18,65
19,66

/**////////////////////////////////////////////////////////////////////////////
[2]数组
数组(C# 编程指南)
数组是一种数据结构,它包含若干相同类型的变量。数组是使用类型声明的:
type[] arrayName;
下面的示例创建一维、多维和交错数组:
1
class TestArraysClass2


{3
static void Main()4

{5
// Declare a single-dimensional array 6
int[] array1 = new int[5];7

8
// Declare and set array element values9

int[] array2 = new int[]
{ 1, 3, 5, 7, 9 };10

11
// Alternative syntax12

int[] array3 =
{ 1, 2, 3, 4, 5, 6 };13

14
// Declare a two dimensional array15
int[,] multiDimensionalArray1 = new int[2, 3];16

17
// Declare and set array element values18

int[,] multiDimensionalArray2 =
{
{ 1, 2, 3 },
{ 4, 5, 6 } };19

20
// Declare a jagged array21
int[][] jaggedArray = new int[6][];22

23
// Set the values of the first array in the jagged array structure24

jaggedArray[0] = new int[4]
{ 1, 2, 3, 4 };25
}26
}三维数组
1
// my code 2
using System;3
using System.Collections.Generic;4
using System.Text;5

6
namespace helloworld7


{8
class Test3D9

{10
public static void Main( )11

{12
int[][,] arr3D = new int[4][ , ];13
// result = 1014
Console.WriteLine(arr3D.Length);15
for (int i = 0; i < arr3D.Length; i++)16

{17
arr3D[i] = new int[3, 2];18
}19
FillArr3D(arr3D, 3, 2);20
PrintArr3D(arr3D, 3, 2);21
}22
static void FillArr3D(int[][,] arr3d, int d2, int d3)23

{24
int dt = 0;25
for (int i = 0; i < arr3d.Length; i++)26
for (int j = 0; j < d2; j++)27
for (int k = 0; k < d3; k++)28
arr3d[i][j, k] = dt++;29
}30
static void PrintArr3D(int[][,] arr3d, int d2, int d3)31

{32
for (int i = 0; i < arr3d.Length; i++)33

{34
for (int j = 0; j < d2; j++)35

{36
for (int k = 0; k < d3; k++)37
Console.Write("[{0}][{1}, {2}] = {3} &&", i, j, k, arr3d[i][j, k]);38
Console.WriteLine();39
}40
Console.WriteLine("__________");41
}42
}43
}44
} 45

/**///////////////////////////// result46
447
[0][0, 0] = 0 &&[0][0, 1] = 1 &&48
[0][1, 0] = 2 &&[0][1, 1] = 3 &&49
[0][2, 0] = 4 &&[0][2, 1] = 5 &&50
__________51
[1][0, 0] = 6 &&[1][0, 1] = 7 &&52
[1][1, 0] = 8 &&[1][1, 1] = 9 &&53
[1][2, 0] = 10 &&[1][2, 1] = 11 &&54
__________55
[2][0, 0] = 12 &&[2][0, 1] = 13 &&56
[2][1, 0] = 14 &&[2][1, 1] = 15 &&57
[2][2, 0] = 16 &&[2][2, 1] = 17 &&58
__________59
[3][0, 0] = 18 &&[3][0, 1] = 19 &&60
[3][1, 0] = 20 &&[3][1, 1] = 21 &&61
[3][2, 0] = 22 &&[3][2, 1] = 23 &&62
__________C#中的二维数组
string[ ,]
string[][]是锯齿数组,两者有本质的区别
1
// 合并数组2
using System;3
using System.Collections.Generic;4
class Dic5


{6
public static void Main()7

{8

int [ ] arrA = new int[]
{1,2,3,4,5};9

int [ ] arrB = new int[]
{6,7,8,9,10};10
int lenA;11
int lenB;12
lenA = arrA.Length;13
lenB = arrB.Length;14
int [ ] arrSum = new int[lenA + lenB];15
Array.Copy( arrA, arrSum, lenA);16
Array.Copy(arrB,0,arrSum,lenA,lenB);17
for ( int i=0; i<arrSum.Length; i++)18

{19
Console.WriteLine( arrSum[i]);20
}21
}22
}23

/**////////////////24
125
226
327
428
529
630
731
832
933
1034

/**//////////////////////////////////////////////////////////[3]
浙公网安备 33010602011771号