zoukankan      html  css  js  c++  java
  • Lua的Table表使用例子(便于使用查询)

    一、table.insert()

    1.1

    1     local countries = {"China", "England", "Brazil"}
    2     --尾插法(Pos不填,默认插入尾部)
    3     table.insert(countries, "France")
    4     --头插法(首部插入)
    5     table.insert(countries, 1, "Australia") 
    6     
    7     dump(countries)

    二、table.remove()

    2.1

    1     local countries = {"China", "England", "Brazil", "England"}
    2     table.removeItem(countries, "England", true)
    3     dump(countries)

    2.2

    1     local countries = {"China", "England", "Brazil", "Australia"}
    2     --移除位置2上的元素(“England”)
    3     table.remove(countries, 2)
    4     --默认尾部删除元素
    5     table.remove(countries)
    6     dump(countries)

    三、table.maxn()

    3.1

    1     local countries = {"China", "England", "Brazil", [7] = "Australia"}
    2     local Cars = {[-3] = "Benz", [-2] = "Toyota", [-1] ="LandRover"}
    3     --获取table的最大下标。如下标小于0,则返回0
    4     print("Countries Maxn  >> "..table.maxn(countries))
    5     print("Cars Maxn  >> "..table.maxn(Cars))
    6     dump(countries)

    四、table.concat()

    4.1

    1     local countries = {"China", "England", "Brazil","Australia"}
    2     --用“+”拼接table中的元素(table[1].."+"..table[2]..………….."+"..table[n])
    3     print("拼接:  "..table.concat(countries, "+"))
    4     print("从Pos[3]开始拼接:  "..table.concat(countries, "+", 3))

    4.2

    1     local countries = {"China", "England", "Brazil","Australia"}
    2     --table.concat(table, "X", began, end)
    3     --从began的位置,用"X"连接到end位置结束
    4     print("拼接:  "..table.concat(countries, "+", 2, 3))

    五、table.sort()

    5.1 

    1     local countries = {"China", "England", "Brazil","Australia"}
    2     table.sort(countries)
    3     dump(countries)

    5.2

     1     local countries = {"China", "England", "Brazil","Australia"}
     2     --自定义排序方式
     3     myComparator = function(a, b) 
     4         if a > b then
     5             return true
     6         else 
     7             return false
     8         end
     9     end 
    10     table.sort(countries, myComparator)
    11     dump(countries)

  • 相关阅读:
    UVA11375
    uva11806(容斥原理)
    uva10325(容斥原理)
    hdu4135(容斥原理)
    CF798
    多线程
    (转载)SVN 提交操作缩写(A D M R) .
    上不了网,如何判断
    (转载)myeclipse项目名称重命名
    mysql模糊查询
  • 原文地址:https://www.cnblogs.com/vokie/p/3804462.html
Copyright © 2011-2022 走看看