posts - 13, comments - 12, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          JavaScript實現Map

          Posted on 2006-07-28 13:16 winder 閱讀(1065) 評論(0)  編輯  收藏 所屬分類: AJAX
          ?1 /*
          ?2 ??name:????????SimpleMap.js
          ?3 ??version:????1.0.0
          ?4 ??author:????yaoly
          ?5 ??date:????????2006-07-26????
          ?6 ??Descript:?This?is?a?Map?implement?of?JavaScript?
          ?7 ??remark:????
          ?8 */
          ?????
          ?9 // SimpleMap?Object?just?like?HashMap?of?Java
          10 function ?SimpleMap() {?????
          11 ???? var ?keys? = ? new ?Array();
          12 ???? var ?entrys? = ? new ?Array();?
          13 ???? this .type? = ?'SimpleMap';?
          14 ???? this .keySet? = ? function () {
          15 ???????? return ?keys;
          16 ????}
          ;??
          17 ???? this .entrySet? = ? function () {
          18 ???????? return ?entrys;
          19 ????}
          ;?
          20 }
          ???????
          21 // put?a?mapping?relation?in?map
          22 SimpleMap.prototype.put? = ? function (key,entry) {?
          23 ????? if (key? != ? null ?
          24 ????????? && ?key? != ?'undefined'
          25 ???????? && ?entry? != ? null
          26 ???????? && ?entry? != ?'undefined') {??
          27 ???????? this .keySet().push(key);????
          28 ???????? this .entrySet().push(entry);????
          29 ?????}

          30 }
          ???????
          31 // return?entry?by?the?gaven?key????value
          32 SimpleMap.prototype.get? = ? function (key) {??
          33 ????? var ?index? = ? this .indexOf(key);
          34 ????? if (index? != ? - 1 ) {
          35 ????????? return ? this .entrySet()[index];
          36 ?????}

          37 ????? return ? null ;?????????
          38 }
          ?
          39 // return?the?size?of?the?map
          40 SimpleMap.prototype.size? = ? function () {????
          41 ???? return ? this .keySet().length;
          42 }
          ??
          43 // clear?all????mapping?records?in?the?map
          44 SimpleMap.prototype.clear? = ? function () {????
          45 ???? while ( this .keySet().length? > ? 0 ) {?
          46 ???????? this .keySet().pop();
          47 ???????? this .entrySet().pop();
          48 ????}

          49 }
          ??
          50 // return?the?index?of?the?key?in?the?map's?keySet?Array?if?unfind?return?-1
          51 SimpleMap.prototype.indexOf? = ? function (key) {?
          52 ???? if (key? != ? null ? && ?key? != ?'undefined') {?
          53 ???????? for ( var ?i = 0 ;i < this .keySet().length;i ++ ) {????
          54 ???????????? if ( this .keySet()[i]? == ?key) {
          55 ???????????????? return ?i;
          56 ????????????}

          57 ????????}
          ????
          58 ????}
          ???????????????????
          59 ???? return ? - 1 ;
          60 }
          ?
          61 // return?a?mapping?record?from?the?map?by?the?gaven?key?value
          62 SimpleMap.prototype.remove? = ? function (key) {
          63 ???? var ?index? = ? this .indexOf(key);
          64 ???? if (index? != ? - 1 ) {?
          65 ???????? var ?tempKey? = ? new ?Array();?
          66 ???????? var ?tempEntry? = ? new ?Array();?
          67 ???????? for ( var ?i = this .keySet().length;i > 0 ;i -- ) {????
          68 ???????????? if (i? != ?(index + 1 )) {?????
          69 ????????????????tempKey.push( this .keySet().pop());
          70 ????????????????tempEntry.push( this .entrySet().pop());
          71 ????????????}
          else {????
          72 ???????????????? this .keySet().pop();
          73 ???????????????? this .entrySet().pop();
          74 ????????????}

          75 ????????}
          ???????????
          76 ???????? for ( var ?i = tempKey.length;i > 0 ;i -- ) {????
          77 ???????????? this .keySet().push(tempKey.pop());
          78 ???????????? this .entrySet().push(tempEntry.pop());?
          79 ????????}
          ??????????
          80 ????}
          ?
          81 }
          ??
          82
          83

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 盱眙县| 呼和浩特市| 韩城市| 板桥市| 石屏县| 台南县| 宁晋县| 裕民县| 鄂伦春自治旗| 和平区| 昔阳县| 自贡市| 马鞍山市| 张家港市| 凤城市| 岳池县| 渝中区| 大石桥市| 海伦市| 新乡县| 伊通| 陈巴尔虎旗| 宝鸡市| 铁岭县| 金溪县| 蓬安县| 拉孜县| 阿拉尔市| 循化| 东丰县| 高密市| 新乡县| 襄城县| 北流市| 栖霞市| 辽宁省| 崇文区| 高台县| 崇州市| 鹤岗市| 潞西市|