创建和使用复合数组

一、使用Object对象创建复合数组

var person=new Object();
person.name="soul";
person.age="21";

二、使用Array对象创建复合数组

var person=new Array();
person["name"]="soul";
person["age"]=21;

三、遍历复合数组

1、for...in  //返回键

2、for each...in  //返回值

四、哈希表

1、代码实现如下:

/**
 *
 * @authors Soul ([email protected])
 * @date    2016-05-23 10:23:49
 * @version 0.0.1
 */

/**
*
* @description  实现哈希表
* @param key  键
* @param value  值
*/

function hashTable(){
    this._hash={};
    this._count=0;

    /**
    *
    * @description  设置或更新键值
    * @return  boolean类型,true/false
    */
    this.put=function(key,value){

        //若键值不存在则键值数量+1
        if(!this._hash.hasOwnProperty(key)){
            this._count++;
        }
        //设置新的或覆盖旧的键值  不会有重复的键值存在
        this._hash[key]=value;
        return true;
    };
    /**
    *
    * @description  获取键指定的值
    * @return  value
    */
    this.get=function(key){
        if(this.containsKey(key))
            return this._hash[key];
    };
    /**
    *
    * @description  获取元素个数
    * @return  _count
    */
    this.size=function(){
        return this._count;
    };
    /**
    *
    * @description  检查是否为空
    * @return  boolean
    */
    this.isEmpty=function(){
        if(this._count==0){
            return true;
        }else{
            return false;
        }
    };
    /**
    *
    * @description  检查是否包含指定的键
    * @param key  键
    * @return  boolean
    */
    this.containsKey=function(key){
        return this._hash.hasOwnProperty(key);
    };
    /**
    *
    * @description  检查是否包含指定的值
    * @param value  值
    * @return  Boolean
    */
    this.containsValue=function(value){
        for(var _key in this._hash){
            if(this._hash[_key]==value){
                return true;
            }
            return false;
        }
    };
    /**
    *
    * @description  删除一组键值
    * @param key  键
    * @return  Boolean
    */
    this.remove=function(key){
        if(this.containsKey(key)){
            delete this._hash[key];
            this.count--;
            return true;
        }
        return false;
    };
    /**
    *
    * @description  清空所有键值
    * @return Boolean
    */
    this.clear=function(){
        this._hash={};
        this._count=0;
        return true;
    };
    /**
    *
    * @description  获取key集合,以数组的形式返回
    * @return Array
    */
    this.keySet=function(){
        var arrKeySet=new Array();
        var index=0;
        for(var _key in this._hash){
            arrKeySet[index++]=_key;
        }
        return (arrKeySet.length===0)?null:arrKeySet;
    };
    /**
    *
    * @description  获取value集合,以数组的形式返回
    * @return  Array
    *
    */
    this.valueSet=function(){
        var arrValueSet=new Array();
        var index=0;
        for(var _key in this._hash){
            arrValueSet[index++]=this._hash[_key];
        }
        return (arrValueSet.length==0)?null:arrValueSet;
    }
}

2、测试如下:

  

时间: 2024-07-30 15:22:04

创建和使用复合数组的相关文章

利用char, str2mat, strvcat创建多行字符串数组

用专门函数char , str2mat , strvcat创建多行字符串数组示例. 1.char('str1', 'str2',...) Convert to character array (string)转换成字符串数组,空字符串是有效的. 示例:S1=char('This string array','', 'has two rows.')   S1 = This string array has two rows. 2.str2mat  (新版本中被char替代) Form blank-

给js创建的一个input数组绑定click事件

</pre><pre name="code" class="javascript"><html> <body> <input type="button" name="input[]" value="按钮1" /><br /> <input type="button" name="input[]&quo

C++中new和delete来创建和释放动态数组

在C++编程中,使用new创建数组然后用delete来释放. 一.创建并释放一维数组 #include<iostream> using namespace std; int main() { int n; cin>>n; //分配动态一维数组 int *arr=new int[n]; for(int i=0;i<n;i++) cin>>arr[i]; for(int i=0;i<n;i++) cout<<arr[i]<<"

创建动态二维数组

1 //======================================================== 2 //创建动态二维数组a [M] [N]: 3 //思路一:二维数组的抽象理解: 4 //思路二:二维数组在内存中实际排列: 5 //======================================================== 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <

创建struct类型的数组

在autoit中,如何创建类似这样的数组呢?如下方式,数组的element只是存储的地址相邻,所以我们可以这样做 $tagMYSTRUCT = "int code; char msg[10];" $mystruct = ArrayStruct($tagMYSTRUCT, 4) $fourth_element = getElement($mystruct, 3, $tagMYSTRUCT) ; $fourth_element is an alias of '$mystruct[3]' D

C++中使用模板,new创建2维动态数组

1 // 使用模板和new创建2维动态数组 2 3 #include<iostream> 4 #include<exception> 5 #include<cstdlib> 6 #include<iomanip> 7 using namespace std; 8 9 template<class Type> 10 bool Make2DArray(Type **&x,int rows,int cols) 11 { 12 int i; 13

new和delete创建和释放动态数组

1.动态创建和释放一维数组 #include<iostream> using namespace std; int main() { int n; cin>>n; //分配动态一维数组 int *arr=new int[n]; for(int i=0;i<n;i++) cin>>arr[i]; for(int i=0;i<n;i++) cout<<arr[i]<<" "; //释放arr数组 delete[] ar

动态创建二维vector数组+指针与引用的区别

二维vectorvector<vector <int> > ivec(m ,vector<int>(n));    //m*n的二维vector 动态创建m*n的二维vector方法一:vector<vector <int> > ivec;ivec.resize(m);for(int i=0;i<m;i++) ivec[i].resize(n); 方法二:vector<vector <int> > ivec;ivec

Java对象与JSON互相转换jsonlib以及手动创建JSON对象与数组——(二)

首先声明一下,jsonlib转换与GSON相比太差劲了,操作不是一般的繁琐.GSON可以直接转换成各种集合与对象类型.强烈推荐使用GSON.而且GSON一个方法就可以解决,jsonlib转来转去太繁琐了. 手动创建JSONObject与JSONArray有用,用于读取文件对字符串进行处理 -----------------------------jsonlib操作复杂,转换Map与list<map>更是复杂---------------- Jar包 User.java 1 package Te