第七章(字典)
来源:互联网 发布:大津算法 opencv 编辑:程序博客网 时间:2024/06/06 01:16
字典是“键-值”结构,也是用数组存储,数组下标就是“键”
//字典类function Dictionary(){//var tag=0;this.datastore=new Array();this.add=add;this.find=find;this.remove=remove;this.showAll=showAll;this.count=count;this.clear=clear;this.sortKey=sortKey;this.sortValue=sortValue;//this.next=next;}////下一个键//function next(){//tag++;//console.log(tag);//if(tag<this.datastore.count) this.tag++;//var flag=0;//for(var key in this.datastore)//{//if(flag==tag) //{console.log(key+"->"+ this.datastore[key]);//break;//}//else flag++;//}//return tag;//}//按键增加function add(key,value){this.datastore[key]=value;}//按键查找function find(key){return this.datastore[key];}//按键删除function remove(key){delete this.datastore[key];}//显示所有键值function showAll(){for(var key in this.datastore){console.log(key+"->"+this.datastore[key]);}}//统计个数function count(){var n=0;//不能用length,因为当键的类型为字符串是,length属性就不管用了//n=this.datastore.length; for(var key in this.datastore) n++;return n;}//清除所有的键值function clear(){for(var key in this.datastore){delete this.datastore[key];}}//按键排序显示,但是并不能对原数组进行排序function sortKey(){//对键值数组来说,sort方法同样无效//对键进行排序,返回的也是新的数组(键,并不包含值),并且对原数组对没有任何影响console.log("按键排序:");var sidc=Object.keys(this.datastore).sort();for(var i=0;i<sidc.length;i++){console.log(sidc[i]+"->"+this.datastore[sidc[i]]);}}//按值排序显示,如果有更好的方法请告知function sortValue(){//创建值数组存放只原数组中的值var i=0;var arry_temp=[];for(var key in this.datastore){arry_temp[i]=this.datastore[key];i++;}//对值数组进行排序arry_temp.sort(function(a,b){return a-b});console.log("对值排序:");//从原数组中找与值数组值相等的for(i=0;i<arry_temp.length;){for(key in this.datastore){if(this.datastore[key]==arry_temp[i]){console.log(key+"->"+this.datastore[key]);i++;}}}}var pbook=new Dictionary();pbook.add("mike","923");pbook.add("mike2","345");pbook.add("mike3","567");pbook.add("mike4","789");pbook.add("amike4","789");console.log(pbook.find("mike2"));pbook.remove("mike2");pbook.showAll();console.log(pbook.count());pbook.sortKey();pbook.sortValue();
阅读全文
0 0
- 第七章(字典)
- 第七章 数据字典
- 数据结构与算法JavaScript描述[第七章](字典)
- UI基础第七弹:字典转模型
- python学习(第七章)
- 第七章(笔记总结)
- Python 基础教程(第七章)
- 第七章 函数(后续)
- 第七章 复用类(上)
- 第七章 复用类(下)
- Java多线程(第七章)
- uva725 除法(第七章)
- 第七章 函数(1)
- 第七章 函数(2)
- 第七章:复用类(上)
- 第七章:复用类(下)
- 数据库基础(第七章)
- 第七周周赛——字典树 + 线段树 + 树状数组等等(去师大比赛前的最后一场)
- Java虚拟机实现安全特性(二)
- String是最基本的数据类型吗?
- java实现二分查找-两种方式
- 数据结构实验之链表五:单链表的拆分
- vue.js开发外卖App项目的组件拆分总结(五)
- 第七章(字典)
- Python(4)list和tuple(类似js里的数组)
- JavaWeb路径问题汇总
- Pyhton、Numpy、Pandas排序小结
- LeetCode19. Remove Nth Node From End of List
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
- MVC
- 类装载器实现Java虚拟机安全(三)
- 【C++】总览