首页 > 其他分享 >table常用用具函数 - list用法

table常用用具函数 - list用法

时间:2023-05-17 23:23:27浏览次数:29  
标签:listTb function end list 用具 return item table

 table_listext.lua

function list.reset(listTb, val)
    for i=1,#listTb do
        listTb[i] = val
    end
end

function list.castItemToNum(listTb)
    for i=1,#listTb do
        listTb[i] = tonumber(listTb[i])
    end
    return listTb
end

function list.castItemToString(listTb)
    for i=1,#listTb do
        listTb[i] = tostring(listTb[i])
    end
    return listTb
end

function list.reverse(listTb)
    local len = #listTb
    local half = math.floor(len * 0.5)

    for i=1,half do
        table.swap(listTb, i, len - i + 1)
    end
end

添加

function list.appendList(listTb, srcListTb)
    for i=1,#srcListTb do
        table.insert(listTb, srcListTb[i])
    end
    return listTb
end

function list.appendTable(listTb, tb)
    for k, v in pairs(tb) do
        table.insert(listTb, v)
    end
    return listTb
end

查找

--从前往后找
function list.indexOfItem(listTb, item)
    for i=1,#listTb do
        if listTb[i] == item then
            return i
        end
    end
    return -1
end

---从后往前找
function list.lastIndexOfItem(listTb, item)
    for i=#listTb,1,-1 do
        if listTb[i] == item then
            return i
        end
    end
    return -1
end

查找匹配

---从前往后找匹配
function list.indexOfMatch(listTb, matchFunc)
    for i=1,#listTb do
        local item = listTb[i]
        if matchFunc(i, item) then
            return i
        end
    end
    return -1
end

---从后往前找匹配
function list.lastIndexOfMatch(listTb, matchFunc)
    for i=#listTb,1,-1 do
        local item = listTb[i]
        if matchFunc(i, item) then
            return i
        end
    end
    return -1
end

删除

---从前往后, 删除第1个相等的
function list.removeFirstItem(listTb, item)
    for i=1,#listTb do
        if listTb[i] == item then
            table.remove(listTb, i)
            return i
        end
    end
    return -1
end

---从后往前, 删除反向第1个相等的
function list.removeLastItem(listTb, item)
    for i=#listTb,1,-1 do
        if listTb[i] == item then
            table.remove(listTb, i)
            return i
        end
    end
end

---删除所有相等的
function list.removeAllItems(listTb, item)
    local count = 0
    for i=#listTb,1,-1 do
        if listTb[i] == item then
            table.remove(listTb, i)
            count = count + 1
        end
    end
    return count
end

删除匹配

---从前往后, 删除第1个匹配的
function list.removeFirstMatch(listTb, matchFunc)
    for i=1,#listTb do
        local item = listTb[i]
        if matchFunc(i, item) then
            table.remove(listTb, i)
            return i, item
        end
    end
    return -1
end

---从后往前, 删除反向第1个匹配的
function list.removeLastMatch(listTb, matchFunc)
    for i=#listTb,1,-1 do
        local item = listTb[i]
        if matchFunc(i, item) then
            table.remove(listTb, i)
            return i, item
        end
    end
end

---删除所有匹配的
function list.removeAllMatch(listTb, matchFunc)
    local count = 0
    for i=#listTb,1,-1 do
        local item = listTb[i]
        if matchFunc(i, item) then
            table.remove(listTb, i)
            count = count + 1
        end
    end
    return count
end

 

标签:listTb,function,end,list,用具,return,item,table
From: https://www.cnblogs.com/sailJs/p/17407684.html

相关文章

  • table常用工具函数 - 表用法
     table_ext.lua---如果table不为空则新建functiontable.getEmptyTable(tb)ifnil==tbornil~=next(tb)thenreturn{}endreturntbendfunctiontable.isEmpty(tb)returnnil==tbornil==next(tb)endfunctiontable.swap(tb,k1......
  • 37、list与Set区别
    (1)List简介实际上有两种List:一种是基本的ArrayList,其优点在于随机访问元素,另一种是LinkedList,它并不是为快速随机访问设计的,而是快速的插入或删除。ArrayList:由数组实现的List。允许对元素进行快速随机访问,但是向List中间插入与移除元素的速度很慢。LinkedList:对顺序访问进行了......
  • datatable中的列头是不区分大小的,== 大写小写均可。
    代码:DataTabledataTable=newDataTable();dataTable.Columns.Add("ID");DataRowdataRow=dataTable.NewRow();dataRow.ItemArray=new[]{"111"};dataTable.Rows.Add(dataRow);Console.WriteLine(dataTable.Rows[0]["id"].ToString());//......
  • Vue ElementUI中 table单元格使用多个Popover解决多行溢出隐藏鼠标悬浮提示问题
    Popover的简单介绍trigger属性用于设置何时触发Popover,支持四种触发方式:hover,click,focus和manual。对于触发Popover的元素,有两种写法:使用slot=“reference”的具名插槽,或使用自定义指令v-popover指向Popover的索引ref。placement弹框的出现位置value/v-model状态......
  • 【React+Antd】 可展开Table
    在antd基础上进行改造,抛弃之前的靠前面+进行展开的方式,在操作列进行点击展开  import{Table}from'antd';importReact,{useState}from'react';import'antd/dist/antd.css';import'./index.css';constAPP=()=>{const[expandedRowKeys,se......
  • 为什么阿里巴巴要求谨慎使用ArrayList中的subList方法
      https://baijiahao.baidu.com/s?id=1637211558024016793&wfr=spider&for=pc 集合是Java开发日常开发中经常会使用到的。在之前的一些文章中,我们介绍过一些关于使用集合类应该注意的事项,如《为什么阿里巴巴禁止在foreach循环里进行元素的remove/add操作》、《为......
  • Java中List集合的addAll方法的小坑
    Java中List集合的addAll方法的小坑遇到的问题已有一个封装类的ArrayList的集合,命名为firstList,现在需要把firstList中的值复制给另一个List,另一个List命名为secondList,然后对secondList中封装类元素中的属性赋值。然后在操作时发现,当使用set方法对secondList的封装类元素赋值时......
  • 记录内网Docker启动Stable-Diffusion遇到的几个坑
    摘要:最近看到K8s启动stable-diffusion的文章,想着在自己开发环境复现一下。没想到在内网环境还遇到这么多问题,记录一下。本文分享自华为云社区《内网Docker启动Stable-Diffusion(AI作画)》,作者:tsjsdbd。最近看到K8s启动stable-diffusion的文章,想着在自己开发环境复现一下。没想到......
  • Row size too large. The maximum row size for the used table type, not counting B
    问题描述新建表或者修改表varchar字段长度的时候,出现这个错误Rowsizetoolarge.Themaximumrowsizefortheusedtabletype,notcountingBLOBs,is65535.Thisincludesstorageoverhead,checkthemanual.YouhavetochangesomecolumnstoTEXTorBLOBs......
  • 【android】Android入门第六篇之ListView
    ListView是一个经常用到的控件,ListView里面的每个子项Item可以是一个字符串,也可以是一个组合控件。先说说ListView的实现:1.准备ListView要显示的数据 ;2.使用 一维或多维 动态数组 保存数据;3.构建适配器 , 简单地来说, 适配器就是 Item数组 , 动态数组 有多少元素就生成......