首页 > 其他 > 详细

遍历列表

时间:2016-04-09 12:20:53      阅读:152      评论:0      收藏:0      [点我收藏+]
    function List() {
        this.listSize = 0;//列表的元素个数,属性
        this.pos = 0;//列表的当前位置,属性
        this.dataStore = []; // 初始化一个空数组来保存列表元素

        this.append = append;//在列表的末尾添加新元素,方法

        this.front = front;//将列表的当前位置设移动到第一个元素,方法
        this.end = end;//将列表的当前位置移动到最后一个元素,方法
        this.prev = prev;//将当前位置前移一位,方法
        this.next = next;//将当前位置后移一位,方法
        this.moveTo = moveTo;//将当前位置移动到指定位置,方法

        this.currPos = currPos;//返回列表的当前位置,方法    
        this.getElement = getElement;//返回当前位置的元素,方法
    }
    function append(element) {
        this.dataStore[this.listSize++] = element;
        //后自加,在新位置添加元素,同时列表的元素个数加1
    }

    function front() {
        this.pos = 0;
    }
    function end() {
        this.pos = this.listSize - 1;
    }
    function prev() {
        if (this.pos > 0) {
            --this.pos;
        }
    }
    function next() {
        if (this.pos < this.listSize - 1) {
            ++this.pos;
        }
    }
    function moveTo(position) {
        this.pos = position;
    }

    function currPos() {
        return this.pos;
    }
    function getElement() {
        return this.dataStore[this.pos];
    }

    var names = new List();
    names.append("Clayton");
    names.append("Raymond");
    names.append("Cynthia");
    names.front();
    alert(names.getElement()); // 显示 Clayton

 

遍历列表

原文:http://www.cnblogs.com/feile/p/5371171.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!