数组、链表、字典、哈希表学习

来源:互联网 发布:c语言double型 编辑:程序博客网 时间:2024/05/24 05:16

        学习这三者时有些困惑,觉得使用起来都可以达到目的,但是在开发中逐渐发现,在不同的情况下使用,对于运行速率是大有不同的,这也再一次告诫了自己,学习需要深刻理解,才能够掌握好知识点。

        特将三者基础感念和异同点总结如下,望各位同道开发者不吝赐教,共同学习。


数组——对象数量较少且数量固定时优先考虑;

字典——需要进行频繁的搜索时优先考虑;

链表——对象数量是动态的且搜索不是优先选项时,需要经常增减节点的情况优先考虑;


(一)数组:

①基础知识点:

1、下标从0开始;

int[], 元素的值默认都是0;
float[], 元素的值默认都是0;
double[],元素的值默认都是0.0;
string[], 元素的值默认都是null;
bool[], 元素的值默认都是false;

2、下标不可以越界;

3、数组名表示数组,不可以进行运算;

4、当需要对数组中的值进行操作时,使用for循环更好;单纯遍历,使用foreach更好。

②优点:

1、数组存储在连续的内存上,索引速度非常快;

2、访问一个元素的时间恒定,与数组的元素数量无关;

3、赋值或修改元素简单。

③缺点:

1、连续存储,在两个元素之间插入新的元素麻烦;

2、容易造成内存浪费(声明的长度过长)或溢出(声明的长度过短)的问题。

一维数组:

using System;  namespace Lesson01 {  class MainClass {     public static void Main ( string[] args ) {  //int[] intArray;                      声明:数据类型 [] 数组名;  //intArray = new int [5];              初始化:数组名 = new 数组类型 [数组长度]   int[] intArray = new int [5];        //声明和初始化二合一:数据类型 [] 数组名 = new 数组类型 [数组长度]   intArray [0] = 8;                    //赋值——数组名 [下标] = 值;  Console.WriteLine (intArray [0]);  //声明、初始化、赋值三合一 数据类型 [] 数组名 = new 数组类型 [数组长度]{值1,...,值N};  //动态赋值——使用new关键字  float[] numbers= new float [5]{2,3,4,5,6};  Console.WriteLine(numbers [0]);  //静态赋值——不使用new关键字  string[] s = {"苏利文","麦克","好好学习","天天向上"};  Console.WriteLine(s [1]);  Console.WriteLine (s [0] + s [1] + s [2] + s [3]);     //可以对数组中的元素进行运算,但是数组名表示数组,不可以进行运算;  //使用循环遍历数组中的元素  //for (int i = 0; i < max; i++) {}                     //快捷操作——for+按Tab两次  //使用foreach遍历输出        foreach(数组数据类型  临时变量名  in 数组名){Console.WriteLine(临时变量名);}  foreach (string ss in s) {  Console.WriteLine (ss);  } //已知数组长度,可以直接决定max范围,从上知max为4  for (int i = 0; i < 4; i++) {            Console.WriteLine(s [i]);  }//使用"数组名.Length"获取数组长度  for (int i = 0; i < s.Length; i++) {            Console.WriteLine(s [i]);  }          }   }  }


二维数组:

using System;namespace Lesson01 {class MainClass {   public static void Main ( string[] args ) { int[,] intArray = new int [2,3];        //声明和初始化二合一:数据类型 [] 数组名 = new 数组类型 [行,列] intArray [1,2] = 3;                     //赋值——数组名 [下标] = 值;Console.WriteLine (intArray [1,2]);//声明、初始化、赋值三合一 数据类型 [] 数组名 = new 数组类型 [数组长度]{{},{}};    int[,] intArray1= new int [2,3]{{1,2,3},{4,5,6}        };    Console.WriteLine(intArray1 [1,2]);  //使用循环遍历数组中的元素for (int i = 0; i < 2; i++) {               //遍历每一行for (int j = 0; j < 3; j++) {       //遍历每一列Console.WriteLine (intArray1 [i, j]);} }//使用foreach遍历输出        foreach(数组数据类型  临时变量名  in 数组名){Console.WriteLine(临时变量名);}foreach (int ss in intArray1 ) {Console.WriteLine (ss);}   } }}


其他补充知识点:

using System;  namespace Lesson01 {  class MainClass {     public static void Main ( string[] args ) {     int[] num1 = new int[]{1,2,3,4,5};    int[] num2 = new int[5]{6,7,8,9,10}; //Array.Copy——将元素从一个数组Copy到另一个数组,后面数字表示到指定位置结束.Array.Copy(num1,num2,4);  foreach(int i in num2)     {   Console.Write(i); }//Reverse——方向输出数组Array.Reverse(num1);foreach (int n in num1) {Console.Write (n);} }}}


(二)字典

①基础知识点:

1、Dictionary是存储键和值的集合;

2、Dictionary是无序的,键Key是唯一的;

3、通过Key找到对应的Value,游戏中通常通过使用ID,ID关联用户的所有信息,从而找到玩家的其他游戏信息。

②优点:

查找速度快,能根据键快速查找值。

③缺点:

内存开销大。

using System.Collections.Generic;//引用泛型集合空间命名    namespace Lesson01 {class MainClass  { public static void Main ( string[] args ) {    Dictionary <string,int> dic = new Dictionary <string,int> ();  //创建一个字典对象,Key类型是string,Value类型是int dic.Add ("Sullivan",23);       //Add添加键值对   dic.Remove("Sullivan");       //从字典中移除键值对   dic.Clear;                    //清空当前字典     int count = dic.Count;        //获取当前字典中KeyValue的个数       Console.WriteLine ("当前字典中有" + count + "个KeyValue");     bool b = dic.ContainsKey("Sun");            //检查字典中是否包含指定的Key        bool c = dic.ContainsValue(22);             //检查字典中是否包含指定的Value  int s;                                      //尝试获取指定的Key所对应的Value  bool bb = dic.TryGetValue ("Sun",out s);    //若当前字典中包含Sun这个Key,则获取对应的Value并保存在s中,bb=true;                                         //若当前字典中不包含Sun这个Key,则s=null,,bb=false;   int age = dic ["Sullivan"];                 //通过Key获取Value            Console.WriteLine (age);              }       }}



(三)链表:

①基础知识点:


②优点:

1、链表在内存存储的排序上不连续,而是靠各对象的指针所决定,添加元素和删除元素比数组优势;

2、链表适合在需要有序的排序的情境下增加新的元素,增加新的元素只是若干元素的指向发生变化。

③缺点:

其在内存空间中不一定连续排列,访问时无法利用下标,必须从头结点开始,逐次遍历下一个节点直到寻找到目标,不适用于需要快速访问对象的情况。



(四)哈希表

①基础知识点:

②优点:

1、数据存储和查找时间短;

2、添加数据效率高。

③缺点:

以空间换时间,需要消耗更多的内存。

using System;using System.Collections;        //使用Hashtable时,必须引入命名空间class Program{public static void Main(){Hashtable ht = new Hashtable();         //创建Hashtable实例ht.Add("福州", "省会");                 //添加keyvalue键值对ht.Add("福大", "南方清华");ht.Add("苏利文", "好学的小白");string capital = (string)ht["福州"];Console.WriteLine(ht.Contains("福大")); //判断哈希表是否包含特定键,其返回值为true或falseConsole.WriteLine(ht["福大"]);ht.Remove("福州");                      //移除一个keyvalue键值对ht.Clear();                             //移除所有元素}}
CSDN上还有很多博主对哈希表有更加详细的讲解,这里推荐两个基础的讲解,想要深入了解的可以阅读更多博主的博客讲解内容。

http://blog.csdn.net/erlian1992/article/details/44887631

http://blog.csdn.net/maggie_j/article/details/52186227

原创粉丝点击