Lua--初学--table常用的公共方法

    科技2022-07-13  129

    1.table.insert(table1_name,table2_name) 将table2_name的值插入到table1_name

    t1={{name="123",age=1},{name="456",age=2}} t2={name="789",age=3} table.insert(t1,t2)

    2.table.remove(table_name,[index]),如果参数列表中没有index的时候,移除的是table中的最后一个元素,有index的时候移除的是table[index]的元素

    table.remove(t1) table.remove(t1,1)

    3.table.sort(table_name,[sortFunction]),如果参数列表没有sortFunction的时候,使用的是默认的sortfunction,升序排序,sortFunction是自定义的排序方法 类似于ICompare中的实现,这里常用于数字排序,或者其他元素的话,要自定义好排序的方法才进行使用

    t3={2,8,9,6,4} table.sort( t3) table.sort( t3, function ( a,b ) if a>b then return true end end )

    4.table.concat(table_name,";",[start_index],[end_index]),拼接一般用于number和string中,返回的是string

    t4={"asd","qwe",456} strdemo=table.concat( t4, ";")

    print strdemo的话会得到asd;qwe;456

    5.Demo

    t1={{name="123",age=1},{name="456",age=2}} t2={name="789",age=3} table.insert(t1,t2) table.remove(t1) table.remove(t1,1) t3={2,8,9,6,4} table.sort( t3) table.sort( t3, function ( a,b ) if a>b then return true end end ) t4={"asd","qwe",456} strdemo=table.concat( t4, ";")
    Processed: 0.008, SQL: 8