zoukankan      html  css  js  c++  java
  • JS模拟list

    /*
     * List 大小可变数组
     */
    function List() {
        this.list = new Array();
    };
    
    /**
     * 将指定的元素添加到此列表的尾部。
     * @param object 指定的元素
     */
    List.prototype.add = function(object) {
        //this.list[this.list.length] = object;
    this.list.push(object);
    };
    
    /**
     * 将List添加到此列表的尾部。
     * @param listObject 一个列表
     */
    List.prototype.addAll = function(listObject) {
        this.list = this.list.concat(listObject.list);
    };
    
    /**
     *  返回此列表中指定位置上的元素。
     * @param index 指定位置
     * @return 此位置的元素
     */
    List.prototype.get = function(index) {
        return this.list[index];
    };
    
    /**
    *  获取元素在数组中的坐标,不存在则返回-1
    * @return true or false
    */
    List.prototype.getObjectIndex = function(object) {    
     var i = 0;    
     for(; i < this.list.length; i++) {
         if( this.list[i] === object) {
            return i;     
         }            
     } 
     return -1;
    };
    
    /**
     * 移除此列表中指定位置上的元素。
     * @param index 指定位置
     * @return 此位置的元素
     */
    List.prototype.removeIndex = function(index) {
        var object = this.list[index];
        this.list.splice(index, 1);    
        return object;
    };
    
    /**
     * 移除此列表中指定元素。
     * @param object 指定元素
     * @return 此位置的元素
     */
    List.prototype.remove = function(object) {
        var i = this.getObjectIndex(object);
        
        if(i==-1) {
            return null;
        } else {
            return this.removeIndex(i);
        }
    };
    
    /**
     * 移除此列表中的所有元素。
     */
    List.prototype.clear = function() {
        this.list.splice(0, this.list.length);
    };
    
    /**
     * 返回此列表中的元素数。
     * @return 元素数量
     */
    List.prototype.size = function() {
        return this.list.length;
    };
    
    /**
     * 返回列表中指定的 start(包括)和 end(不包括)之间列表。
     * @param start 开始位置
     * @param end   结束位置
     * @return  新的列表
     */
    List.prototype.subList = function(start, end) {    
        var list = new List();
        list.list = this.list.slice(start, end);
        return list;
    };
    
    /**
     *  如果列表不包含元素,则返回 true。
     * @return true or false
     */
    List.prototype.isEmpty = function() {
        return this.list.length == 0;
    };
  • 相关阅读:
    从netty源码里拿到的关于http错误码,自己学习下
    9步搞定:用迅雷等工具下载百度网盘资源
    jstack定位cpu高占用
    solr学习笔记section2-solr单机(节点)简单的core操作
    solr学习笔记section1-在tomcat中部署单(节点)机solr5.5.4
    简单排序
    Thrift生成的bean对象,用java内省操作时注意(自己笔记)
    Netty方法误解ChannelHandlerContext.writeAndFlush(Object msg)
    腾讯笔试题,木棍组成多边形判断
    微软笔试题,luckstring
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/3148063.html
Copyright © 2011-2022 走看看