算法学习--动态数组接口设计

动态数组基本接口设置

    function ArrayList(){
        this.arr = [];

        /**
         * 元素数量
         * @param  {[type]} arr [description]
         * @return {[type]}     [description]
         */
        ArrayList.prototype.size = function(){
            return this.arr.length
        }
        /**
         * [isEmpty description]是否为空
         * @param  {[type]}  arr [description]
         * @return {Boolean}     [description]
         */
        ArrayList.prototype.isEmpty = function(){
            let result = this.arr.length > 0 ? false : true;
            return result;
        }
        /**
         * [contains description]是否包含某个元素
         * @param  {[type]} arr [description]
         * @return {[type]}     [description]
         */
        ArrayList.prototype.contains = function(e){
            return this.arr.indexOf(e) > -1;
        }
        /**
         * [add description]添加元素到最后面
         * @param {[type]} e [description]
         */
        ArrayList.prototype.add = function(e){
            this.arr.push(e);
            return this.arr;
        }
        /**
         * [get description]返回index位置对应的元素
         * @param  {[type]} index [description]
         * @return {[type]}       [description]
         */
        ArrayList.prototype.get = function(index){
            return this.arr[index];
        }
        /**
         * [set description]设置index位置的元素
         * @param {[type]} index [description]
         * @param {[type]} e     [description]
         */
        ArrayList.prototype.set = function(index,e){
            this.arr[index] = e;
            return this.arr
        }

        ArrayList.prototype.addViod = function(index,e){
            this.arr.splice(index,0,e);
            return this.arr;
        }
        /**
         * [remove description]删除index位置对应的元素
         * @param  {[type]} index [description]
         * @return {[type]}       [description]
         */
        ArrayList.prototype.remove = function(index){
            this.arr.splice(index,1)
            return this.arr
        }
        /**
         * [indexOf description]查看元素位置
         * @param  {[type]} e [description]
         * @return {[type]}   [description]
         */
        ArrayList.prototype.indexOf = function(e){
            for(let i = 0;i < this.arr.length; i++){
                if(this.arr[i] == e)return i
            }
        }
        /**
         * [clear description]清除所有元素
         * @return {[type]} [description]
         */
        ArrayList.prototype.clear = function(){
            this.arr = [];
            return this.arr;
        }
    }
    
    let arrTest = new ArrayList();
    console.log('add-- ',arrTest.add(1));
    console.log('add-- ',arrTest.add(2));
    console.log('add-- ',arrTest.add(3));
    console.log('add-- ',arrTest.add(4));
    console.log('size-- ',arrTest.size());
    console.log('isEmpty-- ',arrTest.isEmpty());
    console.log('contains-- ',arrTest.contains(4));
    console.log('add-- ',arrTest.add(5));
    console.log('get-- ',arrTest.get(2));
    console.log('set-- ',arrTest.set(2,30));
    console.log('addViod-- ',arrTest.addViod(2,100));
    console.log('remove-- ',arrTest.remove(0));
    console.log('indexOf-- ',arrTest.indexOf(4));
    console.log('clear-- ',arrTest.clear());
算法学习--动态数组接口设计_第1张图片
动态数组.png

你可能感兴趣的:(算法学习--动态数组接口设计)