/*
          name:??? Map.js
          author:? WindDC
          date:??? 2006-10-27
          content: 本程序用JS實現(xiàn)類擬JAVA中MAP對像的功能
          */

          function Node(key,value){//鍵值對對象
          ??? this.key=key;
          ??? this.value=value;
          }

          function Map(){//Map類
          ??? this.nodes=new Array();
          }

          Map.prototype.put=function(key,value){//往容器中加入一個鍵值對
          ??????? for(var i=0;i<this.nodes.length;i++)
          ?????????? if(this.nodes[i].key==key){//如果鍵值已存在,則put方法為更新已有數(shù)據(jù)
          ?????????????? this.nodes[i].value=value;
          ?????????????? return;
          ?????????? }
          ??????? var node=new Node(key,value);
          ??????? this.nodes.push(node);
          ??????? return;
          }//put

          ??
          Map.prototype.get=function(key){//獲取指定鍵的值
          ??????? for(var i=0;i<this.nodes.length;i++)
          ?????????? if(this.nodes[i].key==key)
          ????????????? return this.nodes[i].value;
          ??????? return null;
          }//get
          ????
          Map.prototype.size=function(){//獲取容器中對象的個數(shù)
          ??? ?return this.nodes.length;
          }//size

          ??? ????
          Map.prototype.clear=function(){//清空容器
          ??? ?while(this.nodes.length>0)
          ??? ??? this.nodes.pop();?????
          }//clear
          ?
          Map.prototype.remove=function(key){//刪除指定值
          ??? ?for(var i=0;i<this.nodes.length;i++)
          ??? ??? if(this.nodes[i].key==key){
          ??? ??? ?? if(i>0)
          ??? ????????? var nodes1=this.nodes.concat(this.nodes.slice(0,i-1),this.nodes.slice(i+1));
          ??? ?????? else//刪除的是第一個元素
          ??? ?????? ? var nodes1=nodes.slice(1);
          ??? ?????? this.nodes=nodes1;

          ??? ??? }
          }//remove

          ???
          Map.prototype.isEmpty=function(){//是否為空
          ??? ?if(this.nodes.length==0)
          ??? ?? return true;
          ??? ?else
          ??? ?? return false;
          }//isEmpty
          ???
          Map.prototype.toString=function(){
          ???? var str="[";
          ???? for(var i=0;i<this.nodes.length;i++){
          ??????? if(i<this.nodes.length-1)
          ?????????? str=str+this.nodes[i].key+",";
          ?????? else
          ?????????? str=str+this.nodes[i].key;????
          ?? ?}
          ??? str=str+"]";
          ????return str;
          }

          posted on 2006-10-27 20:06 WindDC 閱讀(320) 評論(0)  編輯  收藏 所屬分類: JS腳本

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


          網(wǎng)站導(dǎo)航:
           
           
          主站蜘蛛池模板: 高陵县| 克什克腾旗| 靖安县| 昂仁县| 健康| 郴州市| 光山县| 车险| 虹口区| 清水县| 汉川市| 兴和县| 溧阳市| 龙南县| 天峨县| 潼关县| 临潭县| 合作市| 江孜县| 洪江市| 商城县| 封丘县| 图们市| 河曲县| 阿合奇县| 大同市| 墨玉县| 广东省| 平谷区| 杂多县| 沂水县| 家居| 海盐县| 广东省| 平昌县| 册亨县| 南投县| 伊吾县| 义乌市| 乐清市| 十堰市|