美文网首页
算法学习--动态数组接口设计

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

作者: 清风徐云去 | 来源:发表于2019-09-25 14:18 被阅读0次

动态数组基本接口设置

    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());
动态数组.png

相关文章

  • 20_总结

    一、动态数组 普通动态数组 环形动态数组 接口设计 int size(){} // 元素的数量 boolean i...

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

    动态数组基本接口设置

  • 二、动态数组简单实现

    1.1设计动态数组: 添加、删除,清空,等接口设计 设计为泛型、这样任何对象都能够使用 动态增加倍数、使用位移运算...

  • 数据结构与算法二(动态数组)

    目录一、什么是数据结构?二、线性表2.1 数组(Array)2.2 动态数组(Dynamic Array)接口设计...

  • 浅谈动态数组&数据结构(object-C)

    什么是数据结构? 接下来我们手写一个动态数组 首先动态数组的接口设计如下: 实现代码如下: #import ...

  • Java数据结构_新的征程_动态数组

    本文目标 理解动态数组,并能够实现简易的动态数组数据结构 先明确两个概念 扩容的思想 缩容的思想 顶层接口设计 基...

  • 高频题

    字符串 数学 数组 二叉树 二分查找 链表 动态规划 贪心算法 堆 广度优先搜索 深度优先 哈希表 回溯算法 设计

  • 源码阅读 - ArrayList

    0. ArrayList是什么 动态数组 1. 实现的本质 动态数组的本质当然是数组 2.常用接口 2.1 构造函...

  • List接口(动态数组)

    1.List接口的三个实现类:image.png 2.Arraylist的原码分析 image.pngimage....

  • ArrayList和LinkList的区别

    ArrayList:是Array的数据结构,Array是动态数组,是对List接口的实现,他是数组队列,相当于动态...

网友评论

      本文标题:算法学习--动态数组接口设计

      本文链接:https://www.haomeiwen.com/subject/dpaluctx.html