盘点几种常见的数据结构

1.几种常见的数据结构
这里主要总结下在工作中常碰到的几种数据结构:array,arraylist,list,linkedlist,queue,stack,dictionary。,t>
数组array:
数组是最简单的数据结构。其具有如下特点:
数组存储在连续的内存上。
数组的内容都是相同类型。
数组可以直接通过下标访问。
数组array的创建:
1 int size = 5; 2 int[] test = new int[size];
创建一个新的数组时将在 clr 托管堆中分配一块连续的内存空间,来盛放数量为size,类型为所声明类型的数组元素。如果类型为值类型,则将会有size个未装箱的该类型的值被创建。如果类型为引用类型,则将会有size个相应类型的引用被创建。
由于是在连续内存上存储的,所以它的索引速度非常快,访问一个元素的时间是恒定的也就是说与数组的元素数量无关,而且赋值与修改元素也很简单。
string[] test2 = new string[3];//赋值test2[0] = chen;test2[1] = j;test2[2] = d;//修改test2[0] = chenjd; 但是有优点,那么就一定会伴随着缺点。由于是连续存储,所以在两个元素之间插入新的元素就变得不方便。而且就像上面的代码所显示的那样,声明一个新的数组时,必须指定其长度,这就会存在一个潜在的问题,那就是当我们声明的长度过长时,显然会浪费内存,当我们声明长度过短的时候,则面临这溢出的风险。这就使得写代码像是投机,很厌恶这样的行为!针对这种缺点,下面隆重推出arraylist。
arraylist:
为了解决数组创建时必须指定长度以及只能存放相同类型的缺点而推出的数据结构。arraylist是system.collections命名空间下的一部分,所以若要使用则必须引入system.collections。正如上文所说,arraylist解决了数组的一些缺点。
不必在声明arraylist时指定它的长度,这是由于arraylist对象的长度是按照其中存储的数据来动态增长与缩减的。
arraylist可以存储不同类型的元素。这是由于arraylist会把它的元素都当做object来处理。因而,加入不同类型的元素是允许的。
arraylist的操作:
arraylist test3 = new arraylist();//新增数据test3.add(chen);test3.add(j);test3.add(d);test3.add(is);test3.add(25);//修改数据test3[4] = 26;//删除数据test3.removeat(4); 说了那么一堆”优点“,也该说说缺点了吧。为什么要给”优点”打上引号呢?那是因为arraylist可以存储不同类型数据的原因是由于把所有的类型都当做object来做处理,也就是说arraylist的元素其实都是object类型的,辣么问题就来了。
arraylist不是类型安全的。因为把不同的类型都当做object来做处理,很有可能会在使用arraylist时发生类型不匹配的情况。
如上文所诉,数组存储值类型时并未发生装箱,但是arraylist由于把所有类型都当做了object,所以不可避免的当插入值类型时会发生装箱操作,在索引取值时会发生拆箱操作。这能忍吗?
注:为何说频繁的没有必要的装箱和拆箱不能忍呢?所谓装箱 (boxing):就是值类型实例到对象的转换(百度百科)。那么拆箱:就是将引用类型转换为值类型咯(还是来自百度百科)。下面举个栗子~
//装箱,将string类型的值fanyoychenjd赋值给对象。int info = 1989; object obj=(object)info; //拆箱,从obj中提取值给infoobject obj = 1;int info = (int)obj; 那么结论呢?显然,从原理上可以看出,装箱时,生成的是全新的引用对象,这会有时间损耗,也就是造成效率降低。
list泛型list
为了解决arraylist不安全类型与装箱拆箱的缺点,所以出现了泛型的概念,作为一种新的数组类型引入。也是工作中经常用到的数组类型。和arraylist很相似,长度都可以灵活的改变,最大的不同在于在声明list集合时,我们同时需要为其声明list集合内数据的对象类型,这点又和array很相似,其实list内部使用了array来实现。
list test4 = new list(); //新增数据 test4.add(“fanyoy”); test4.add(“chenjd”); //修改数据 test4[1] = “murongxiaopifu”; //移除数据 test4.removeat(0); 这么做最大的好处就是
即确保了类型安全。
也取消了装箱和拆箱的操作。
它融合了array可以快速访问的优点以及arraylist长度可以灵活变化的优点。
linkedlist
也就是链表了。和上述的数组最大的不同之处就是在于链表在内存存储的排序上可能是不连续的。这是由于链表是通过上一个元素指向下一个元素来排列的,所以可能不能通过下标来访问。如图

既然链表最大的特点就是存储在内存的空间不一定连续,那么链表相对于数组最大优势和劣势就显而易见了。
向链表中插入或删除节点无需调整结构的容量。因为本身不是连续存储而是靠各对象的指针所决定,所以添加元素和删除元素都要比数组要有优势。
链表适合在需要有序的排序的情境下增加新的元素,这里还拿数组做对比,例如要在数组中间某个位置增加新的元素,则可能需要移动移动很多元素,而对于链表而言可能只是若干元素的指向发生变化而已。
有优点就有缺点,由于其在内存空间中不一定是连续排列,所以访问时候无法利用下标,而是必须从头结点开始,逐次遍历下一个节点直到寻找到目标。所以当需要快速访问对象时,数组无疑更有优势。
综上,链表适合元素数量不固定,需要经常增减节点的情况。
示例:
下面的代码示例演示了类的许多功能linkedlist。
using system;using system.text;using system.collections.generic;public class example{ public static void main() { // create the link list. string[] words = { the, fox, jumps, over, the, dog }; linkedlist sentence = new linkedlist(words); display(sentence, the linked list values:); console.writeline(sentence.contains(\jumps\) = {0}, sentence.contains(jumps)); // add the word 'today' to the beginning of the linked list. sentence.addfirst(today); display(sentence, test 1: add 'today' to beginning of the list:); // move the first node to be the last node. linkedlistnode mark1 = sentence.first; sentence.removefirst(); sentence.addlast(mark1); display(sentence, test 2: move first node to be last node:); // change the last node to 'yesterday'. sentence.removelast(); sentence.addlast(yesterday); display(sentence, test 3: change the last node to 'yesterday':); // move the last node to be the first node. mark1 = sentence.last; sentence.removelast(); sentence.addfirst(mark1); display(sentence, test 4: move last node to be first node:); // indicate the last occurence of 'the'. sentence.removefirst(); linkedlistnode current = sentence.findlast(the); indicatenode(current, test 5: indicate last occurence of 'the':); // add 'lazy' and 'old' after 'the' (the linkedlistnode named current). sentence.addafter(current, old); sentence.addafter(current, lazy); indicatenode(current, test 6: add 'lazy' and 'old' after 'the':); // indicate 'fox' node. current = sentence.find(fox); indicatenode(current, test 7: indicate the 'fox' node:); // add 'quick' and 'brown' before 'fox': sentence.addbefore(current, quick); sentence.addbefore(current, brown); indicatenode(current, test 8: add 'quick' and 'brown' before 'fox':); // keep a reference to the current node, 'fox', // and to the previous node in the list. indicate the 'dog' node. mark1 = current; linkedlistnode mark2 = current.previous; current = sentence.find(dog); indicatenode(current, test 9: indicate the 'dog' node:); // the addbefore method throws an invalidoperationexception // if you try to add a node that already belongs to a list. console.writeline(test 10: throw exception by adding node (fox) already in the list:); try { sentence.addbefore(current, mark1); } catch (invalidoperationexception ex) { console.writeline(exception message: {0}, ex.message); } console.writeline(); // remove the node referred to by mark1, and then add it // before the node referred to by current. // indicate the node referred to by current. sentence.remove(mark1); sentence.addbefore(current, mark1); indicatenode(current, test 11: move a referenced node (fox) before the current node (dog):); // remove the node referred to by current. sentence.remove(current); indicatenode(current, test 12: remove current node (dog) and attempt to indicate it:); // add the node after the node referred to by mark2. sentence.addafter(mark2, current); indicatenode(current, test 13: add node removed in test 11 after a referenced node (brown):); // the remove method finds and removes the // first node that that has the specified value. sentence.remove(old); display(sentence, test 14: remove node that has the value 'old':); // when the linked list is cast to icollection(of string), // the add method adds a node to the end of the list. sentence.removelast(); icollection icoll = sentence; icoll.add(rhinoceros); display(sentence, test 15: remove last node, cast to icollection, and add 'rhinoceros':); console.writeline(test 16: copy the list to an array:); // create an array with the same number of // elements as the linked list. string[] sarray = new string[sentence.count]; sentence.copyto(sarray, 0); foreach (string s in sarray) { console.writeline(s); } // release all the nodes. sentence.clear(); console.writeline(); console.writeline(test 17: clear linked list. contains 'jumps' = {0}, sentence.contains(jumps)); console.readline(); } private static void display(linkedlist words, string test) { console.writeline(test); foreach (string word in words) { console.write(word + ); } console.writeline(); console.writeline(); } private static void indicatenode(linkedlistnode node, string test) { console.writeline(test); if (node.list == null) { console.writeline(node '{0}' is not in the list.\n, node.value); return; } stringbuilder result = new stringbuilder(( + node.value + )); linkedlistnode nodep = node.previous; while (nodep != null) { result.insert(0, nodep.value + ); nodep = nodep.previous; } node = node.next; while (node != null) { result.append( + node.value); node = node.next; } console.writeline(result); console.writeline(); }}//this code example produces the following output:////the linked list values://the fox jumps over the dog//test 1: add 'today' to beginning of the list://today the fox jumps over the dog//test 2: move first node to be last node://the fox jumps over the dog today//test 3: change the last node to 'yesterday'://the fox jumps over the dog yesterday//test 4: move last node to be first node://yesterday the fox jumps over the dog//test 5: indicate last occurence of 'the'://the fox jumps over (the) dog//test 6: add 'lazy' and 'old' after 'the'://the fox jumps over (the) lazy old dog//test 7: indicate the 'fox' node://the (fox) jumps over the lazy old dog//test 8: add 'quick' and 'brown' before 'fox'://the quick brown (fox) jumps over the lazy old dog//test 9: indicate the 'dog' node://the quick brown fox jumps over the lazy old (dog)//test 10: throw exception by adding node (fox) already in the list://exception message: the linkedlist node belongs a linkedlist.//test 11: move a referenced node (fox) before the current node (dog)://the quick brown jumps over the lazy old fox (dog)//test 12: remove current node (dog) and attempt to indicate it://node 'dog' is not in the list.//test 13: add node removed in test 11 after a referenced node (brown)://the quick brown (dog) jumps over the lazy old fox//test 14: remove node that has the value 'old'://the quick brown dog jumps over the lazy fox//test 15: remove last node, cast to icollection, and add 'rhinoceros'://the quick brown dog jumps over the lazy rhinoceros//test 16: copy the list to an array://the//quick//brown//dog//jumps//over//the//lazy//rhinoceros//test 17: clear linked list. contains 'jumps' = false// queue
在queue这种数据结构中,最先插入在元素将是最先被删除;反之最后插入的元素将最后被删除,因此队列又称为“先进先出”(fifo—first in first out)的线性表。通过使用enqueue和dequeue这两个方法来实现对 queue 的存取。
一些需要注意的地方:
先进先出的情景。
默认情况下,queue的初始容量为32, 增长因子为2.0。
当使用enqueue时,会判断队列的长度是否足够,若不足,则依据增长因子来增加容量,例如当为初始的2.0时,则队列容量增长2倍。
乏善可陈。
示例:
下面的代码示例演示了泛型类的几个方法 queue 。 此代码示例创建具有默认容量的字符串队列,并使用 enqueue 方法将五个字符串进行排队。 枚举队列的元素,而不会更改队列的状态。 dequeue方法用于取消对第一个字符串的排队。 peek方法用于查看队列中的下一项,然后 dequeue 使用方法将其取消排队。
toarray方法用于创建数组,并将队列元素复制到该数组,然后将数组传递给 queue 采用的构造函数 ienumerable ,创建队列的副本。 将显示副本的元素。
创建队列大小两倍的数组,并 copyto 使用方法从数组中间开始复制数组元素。 在 queue 开始时,将再次使用构造函数来创建包含三个 null 元素的队列的第二个副本。
contains方法用于显示字符串 四 位于队列的第一个副本中,在此之后, clear 方法会清除副本, count 属性显示该队列为空。
using system;using system.collections.generic;class example{ public static void main() { queue numbers = new queue(); numbers.enqueue(one); numbers.enqueue(two); numbers.enqueue(three); numbers.enqueue(four); numbers.enqueue(five); // a queue can be enumerated without disturbing its contents. foreach( string number in numbers ) { console.writeline(number); } console.writeline(\ndequeuing '{0}', numbers.dequeue()); console.writeline(peek at next item to dequeue: {0}, numbers.peek()); console.writeline(dequeuing '{0}', numbers.dequeue()); // create a copy of the queue, using the toarray method and the // constructor that accepts an ienumerable. queue queuecopy = new queue(numbers.toarray()); console.writeline(\ncontents of the first copy:); foreach( string number in queuecopy ) { console.writeline(number); } // create an array twice the size of the queue and copy the // elements of the queue, starting at the middle of the // array. string[] array2 = new string[numbers.count * 2]; numbers.copyto(array2, numbers.count); // create a second queue, using the constructor that accepts an // ienumerable(of t). queue queuecopy2 = new queue(array2); console.writeline(\ncontents of the second copy, with duplicates and nulls:); foreach( string number in queuecopy2 ) { console.writeline(number); } console.writeline(\nqueuecopy.contains(\four\) = {0}, queuecopy.contains(four)); console.writeline(\nqueuecopy.clear()); queuecopy.clear(); console.writeline(\nqueuecopy.count = {0}, queuecopy.count); }}/* this code example produces the following output:onetwothreefourfivedequeuing 'one'peek at next item to dequeue: twodequeuing 'two'contents of the copy:threefourfivecontents of the second copy, with duplicates and nulls:threefourfivequeuecopy.contains(four) = truequeuecopy.clear()queuecopy.count = 0 */ stack
与queue相对,当需要使用后进先出顺序(lifo)的数据结构时,我们就需要用到stack了。
一些需要注意的地方:
后进先出的情景。
默认容量为10。
使用pop和push来操作。
乏善可陈。
示例:
下面的代码示例演示了泛型类的几个方法 stack 。 此代码示例创建一个具有默认容量的字符串堆栈,并使用 push 方法将五个字符串推送到堆栈上。 枚举堆栈的元素,这不会更改堆栈的状态。 pop方法用于弹出堆栈中的第一个字符串。 peek方法用于查看堆栈上的下一项,然后 pop 使用方法将它弹出。
toarray方法用于创建数组,并将堆栈元素复制到该数组,然后将该数组传递给 stack 采用的构造函数,并 ienumerable 使用反转元素的顺序创建堆栈副本。 将显示副本的元素。
创建堆栈大小两倍的数组,并 copyto 使用方法从数组中间开始复制数组元素。 stack再次使用构造函数来创建具有反转的元素顺序的堆栈副本; 因此,这三个 null 元素位于末尾。
contains方法用于显示字符串 四 位于堆栈的第一个副本中,在此之后, clear 方法会清除副本, count 属性会显示堆栈为空。
using system;using system.collections.generic;class example{ public static void main() { stack numbers = new stack(); numbers.push(one); numbers.push(two); numbers.push(three); numbers.push(four); numbers.push(five); // a stack can be enumerated without disturbing its contents. foreach( string number in numbers ) { console.writeline(number); } console.writeline(\npopping '{0}', numbers.pop()); console.writeline(peek at next item to destack: {0}, numbers.peek()); console.writeline(popping '{0}', numbers.pop()); // create a copy of the stack, using the toarray method and the // constructor that accepts an ienumerable. stack stack2 = new stack(numbers.toarray()); console.writeline(\ncontents of the first copy:); foreach( string number in stack2 ) { console.writeline(number); } // create an array twice the size of the stack and copy the // elements of the stack, starting at the middle of the // array. string[] array2 = new string[numbers.count * 2]; numbers.copyto(array2, numbers.count); // create a second stack, using the constructor that accepts an // ienumerable(of t). stack stack3 = new stack(array2); console.writeline(\ncontents of the second copy, with duplicates and nulls:); foreach( string number in stack3 ) { console.writeline(number); } console.writeline(\nstack2.contains(\four\) = {0}, stack2.contains(four)); console.writeline(\nstack2.clear()); stack2.clear(); console.writeline(\nstack2.count = {0}, stack2.count); }}/* this code example produces the following output:fivefourthreetwoonepopping 'five'peek at next item to destack: fourpopping 'four'contents of the first copy:onetwothreecontents of the second copy, with duplicates and nulls:onetwothreestack2.contains(four) = falsestack2.clear()stack2.count = 0 */ dictionary,t>
提到字典就不得不说hashtable哈希表以及hashing(哈希,也有叫散列的),因为字典的实现方式就是哈希表的实现方式,只不过字典是类型安全的,也就是说当创建字典时,必须声明key和item的类型,这是第一条字典与哈希表的区别。关于哈希,简单的说就是一种将任意长度的消息压缩到某一固定长度,比如某学校的学生学号范围从00000~99999,总共5位数字,若每个数字都对应一个索引的话,那么就是100000个索引,但是如果我们使用后3位作为索引,那么索引的范围就变成了000~999了,当然会冲突的情况,这种情况就是哈希冲突(hash collisions)了。
回到dictionary,我们在对字典的操作中各种时间上的优势都享受到了,那么它的劣势到底在哪呢?对嘞,就是空间。以空间换时间,通过更多的内存开销来满足我们对速度的追求。在创建字典时,我们可以传入一个容量值,但实际使用的容量并非该值。而是使用“不小于该值的最小质数来作为它使用的实际容量,最小是3。”(老赵),当有了实际容量之后,并非直接实现索引,而是通过创建额外的2个数组来实现间接的索引,即int[] buckets和entry[] entries两个数组(即buckets中保存的其实是entries数组的下标),这里就是第二条字典与哈希表的区别,还记得哈希冲突吗?对,第二个区别就是处理哈希冲突的策略是不同的!字典会采用额外的数据结构来处理哈希冲突,这就是刚才提到的数组之一buckets桶了,buckets的长度就是字典的真实长度,因为buckets就是字典每个位置的映射,然后buckets中的每个元素都是一个链表,用来存储相同哈希的元素,然后再分配存储空间。,t>

因此,我们面临的情况就是,即便我们新建了一个空的字典,那么伴随而来的是2个长度为3的数组。所以当处理的数据不多时,还是慎重使用字典为好,很多情况下使用数组也是可以接受的。
几种常见数据结构的使用情景
array 需要处理的元素数量确定并且需要使用下标时可以考虑,不过建议使用list
arraylist 不推荐使用,建议用list
list泛型list 需要处理的元素数量不确定时 通常建议使用
linkedlist 链表适合元素数量不固定,需要经常增减节点的情况,2端都可以增减
queue 先进先出的情况
stack 后进先出的情况
dictionary 需要键值对,快速操作



季丰ATE实验室引进高端测试机93k STH测试机
干货|电路设计中如何减少ESD?
诺基亚6评测 由内到外焕然一新千元市场新秀
怎样入门数据工程师
语音助手正日益成为我们日常生活的一部分
盘点几种常见的数据结构
美国柏恩推出微机电系统(MEMS)环境传感器
三星S10首销上演王者归来,苏宁门店被挤爆!
为强化智能电网建设实施“外电入鲁”,山东将展开哪些工作?
高通正式推出首款商用5G移动平台骁龙855
巨头云集的异构集成
LED企业携手电商 “立体式营销”成趋势
消息称瑞萨将退出微波市场转战光器件
NOR Flash厂商兆易创新将与ISSI整并
曝微软计划在Windows 10 2004引入针对文件资源管理器的重大改进 预计正式版在5月份前后推送
优派升级两款中端显示器:四边窄边框,内置有线网口
使能万物智联新时代 广和通燃爆2023深圳国际物联网展
台行政院副院长:未来将投6207亿给半导体产业
你知道什么是3D眼镜吗
功能强大的美容仪设计给人更好的舒适感和快感