zoukankan      html  css  js  c++  java
  • js中创建Map集合

    function Map() { //创建一个map集合

    //这里定义数据结构
    var struct = function(key, value) {  
    this.key = key;
    this.value = value;
    }

    //这是添加的方法

    var put = function(key, value){
    for (var i = 0; i < this.arr.length; i++) {
    if ( this.arr[i].key === key ) {
    this.arr[i].value = value;
    return;
    }
    }
    this.arr[this.arr.length] = new struct(key, value);
    }

    //获取的方法

    var get = function(key) {
    for (var i = 0; i < this.arr.length; i++) {
    if ( this.arr[i].key === key ) {
    return this.arr[i].value;
    }
    }
    return null;
    }
    //这种是根据自己的需求来定义的方法
    var getIndex = function(index){
    if(this.arr.length-1>=index){
    return this.arr[index].key;
    }
    return null;
    }

    //删除的方法

    var remove = function(key) {
    var v;
    for (var i = 0; i < this.arr.length; i++) {
    v = this.arr.pop();
    if ( v.key*1 === key*1 ) {
    continue;
    }
    this.arr.unshift(v);
    }
    }

    //获取存储的数量

    var size = function() {
    return this.arr.length;
    }

    //判断是否为空

    var isEmpty = function() {
    return this.arr.length <= 0;
    }
    //清空
    var toNull=function(){
    this.arr=new Array();
    }

    //发布服务

    this.arr = new Array();
    this.get = get;
    this.put = put;
    this.toNull=toNull;
    this.remove = remove;
    this.getIndex =getIndex;
    this.getValue=getValue;
    this.size = size;
    this.isEmpty = isEmpty;
    }

  • 相关阅读:
    沈阳集训day2
    ac自动机
    2018沈阳集训day1
    洛谷P1875 佳佳的魔法药水
    洛谷P1941 飞扬的小鸟
    Noip2016day2
    1123: [POI2008]BLO
    1718: [Usaco2006 Jan] Redundant Paths 分离的路径
    P3119 [USACO15JAN]草鉴定Grass Cownoisseur
    [LeetCode] Clone Graph
  • 原文地址:https://www.cnblogs.com/xielinjiang/p/9146220.html
Copyright © 2011-2022 走看看