美文网首页
使用PHP创建列表类

使用PHP创建列表类

作者: zooeymoon | 来源:发表于2018-07-23 02:43 被阅读0次

创建列表类

<?php
/**
 * Created by PhpStorm.
 * User: zooeymoon
 * Date: 2018/5/6
 * Time: 15:06
 */

class Lists{

    public $elements = [];
    public $pos;

    public function __construct()
    {
        $this->pos = 0;
    }

    /**
     * 清空列表元素
     */
    public function clear()
    {
        $this->elements = [];
        $this->pos = 0;
    }

    /**
     * 向列表中添加元素
     * @param $element 元素
     */
    public function append($element)
    {
        $this->elements[$this->getSize()] = $element;
    }

    /**
     * 输出列表元素
     */
    public function toString()
    {
        echo implode(",",$this->elements);
    }

    /**
     * 删除列表中的元素
     * 成功返回true
     * @param $element 要删除的元素
     * @return bool
     */
    public function remove($element)
    {
        $foundAt = $this->find($element);

        if($foundAt > -1){
            array_splice($this->elements,$foundAt,1);
            return true;
        }

        return false;
    }


    /**
     * 查找元素是否在列表中存在
     * 存在返回元素位置,不存在返回-1
     * @param $element
     * @return int
     */
    public function find($element)
    {
        for($i = 0 ; $i < $this->getSize() ; $i++){
            if($this->elements[$i] == $element){
                return $i;
            }
        }

        return -1;
    }

    /**
     * 返回列表元素的数量
     * @return int
     */
    public function getSize()
    {
        return count($this->elements);
    }

    /**
     * 返回当前列表指针位置
     * @return int
     */
    public function getCurrentPosition()
    {
        return $this->pos + 1;
    }

    /**
     * 插入元素
     * @param $element
     * @param $after
     * @return bool
     */
    public function insert($element,$after)
    {
        $insertPos = $this->find($after);

        if($insertPos > -1){
            array_splice($this->elements,$insertPos+1,0,$element);
            return true;
        }

        return false;
    }

    /***
     * 查找某个元素是否在列表中
     * @param $element
     * @return bool
     */
    public function contains($element)
    {

        return in_array($element,$this->elements);

    }

    public function front()
    {
        $this->pos = 0;
    }

    public function end()
    {
        $this->pos = count($this->elements)-1;
    }

    public function prev()
    {
        if($this->hasPrev()){
            $this->pos--;
            return true;
        }

        return false;
    }

    public function next()
    {
        if($this->hasNext()){
            $this->pos++;
            return true;
        }

        return false;
    }

    public function hasNext()
    {
        return $this->pos < count($this->elements)-1;
    }

    public function hasPrev()
    {
        return $this->pos > 0;
    }

    public function moveTo($position){

        if($position > 0){

            if($position-1 <= count($this->elements)){
                $this->pos = $position - 1;
                return true;
            }
        }

        return false;

    }

    public function getElement()
    {
        return $this->elements[$this->pos];
    }


}

class Person{
    public $name;
    public $sex;

    public function __construct($name,$sex)
    {
        $this->name = $name;
        $this->sex = $sex;
    }
}


$list = new Lists();
$list->append(new Person("liwenqiang",1));
$list->append(new Person("haha",1));
$list->append(new Person("whatever",2));
$list->append(new Person("hehe",1));

function getSameSexName($list , $sex){
    for($list->front();$list->hasNext();$list->next()){
        if($list->getElement()->sex == $sex){
            echo $list->getElement()->name;
        }
    }
}

getSameSexName($list,1);

相关文章

  • 使用PHP创建列表类

    创建列表类

  • PHP 依赖注入容器

    介绍 使用 PHP 的反射类 ReflectionClass,创建容器方便管理依赖注入。 代码 创建 digita...

  • Laravel队列及supervisor

    队列 参考文档 使用 创建失败数据库 queue.php配置 SendSmsJob类 调用 测试php artis...

  • laravel中的三种验证

    控制器直接使用validate方法 单独创建验证类语法:php artisan make:request 验证类名...

  • php面向对象相关技术总结

    php内置标准类 基本介绍php中有一个类 stdClass,不需要创建就可以使用,通常可以使用它来以对象的形式管...

  • 分页和翻页 - Pagination And Pager

    怎样创建分页组件? 为列表元素 添加一个 .pagination 类即可创建分页组件,使用 .disabled...

  • PHP基础 -- 类自动载入

    使用PHP标准库SPL,中的自动载入功能,自动require类文件 创建4个文件 index.php主入口文件 C...

  • python 列表

    列表 列表是通常用来存储相同元素的集合。 a、创建列表 使用中括号[ ]来创建列表,列表中的元素使用逗号(,)分隔...

  • PHP中的面向对象

    PHP面向对象的语法,面向对象只可意会不可言传,万物皆对象: 定义类 创建类对象(实例化) 使用对象 对象创建方式...

  • Node.js 撸第一个Web应用

    使用Node.js 创建Web 应用与使用PHP/Java 语言创建Web应用略有不同。使用PHP/Java 来编...

网友评论

      本文标题:使用PHP创建列表类

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