zoukankan      html  css  js  c++  java
  • Lua table总结




    1、table基础

    table概念之类的基础东西可以自行百度学习,这里主要总结一些用法什么的。

    1.1、table索引

    tab1 = {color = "red",{x=0},{x=1}}
    
    --区分tab1.x和tab1[x]
    tab1.x = tab1["x"]     --用x字符串索引表
    tab1[x]                --用x的值索引表
    
    
    print(tab1[2].x)       --输出1
    --用[]索引时,键值对形式的项,不会被计数
    

    1.2、table遍历

    三种遍历方式介绍。

    
    tab1 = {"a",length=333,"b","c"}
    
    --=================================================
    --最简单的方式(不能遍历键值对)
    for i=1,3 do 
    	print(tab1[i])
    end
    
    --[[
    a
    b
    c
    ]]
    
    --=================================================
    --ipairs迭代器(也不能遍历键值对)
    for k,v in ipairs(tab1) do
    	print(k .. "   " .. v)
    end
    
    --[[
    1   a
    2   b
    3   c
    ]]
    
    --=================================================
    
    --pairs迭代器(最常用的,可遍历所有)
    
    for k,v in pairs(tab1) do
    	print(k .. "   " .. v)
    end
    
    --[[
    1   a
    2   b
    3   c
    length   333
    ]]
    
    --注意:如果遍历键值对时,那么遍历顺序不能完全保证按照顺序来
    --比如:
    tab2 = {a=1,b=2,c=3,d=4}
    for k,v in pairs(tab2) do
    	print(k .. "   " .. v)
    end
    --[[
    a   1
    d   4
    c   3
    b   2
    ]]
    
    

    1.3、table方法

    table.insert (table, [pos,] value)
    在table数组指定位置插入一个元素。

    table.remove (table [, pos])
    删除table数组位于pos位置的元素, 其后的元素会被前移。

    table.sort (table [, comp])
    对给定的table进行升序排序。

    table.getn()
    返回table中元素的个数

    table.concat(table, sep, start, end)
    连接table表中的元素,以sep为分隔符
    除了table外, 其他的参数都不是必须的, 分隔符的默认值是空字符, start的默认值是1, end的默认值是数组部分的总长。

    table.insert()

    tab1 = {"a","b","c"}
    
    --位置可选,默认为最后面。
    table.insert(tab1,2,222)
    
    for k,v in pairs(tab1) do
    	print(k .. "   " .. v)
    end
    
    --[[
    1   a
    2   222
    3   b
    4   c
    ]]
    

    table.remove()

    tab1 = {"a","b","c"}
    
    --位置可选,默认为最后面。
    table.remove(tab1,2)
    
    for k,v in pairs(tab1) do
    	print(k .. "   " .. v)
    end
    
    --[[
    1   a
    2   c
    ]]
    

    table.sort()

    tab1 = {"b","a","c"}
    
    --默认对数组进行升序排列
    table.sort(tab1)
    
    for k,v in pairs(tab1) do
    	print(k .. "   " .. v)
    end
    
    --[[
    1   a
    2   b
    3   c
    ]]
    
    
    student = 
    {
    	{name = "a",grade = 80},
    	{name = "b",grade = 100},
    	{name = "c",grade = 60},
    }
    
    --自定义函数排列
    table.sort(student,function(s1,s2) return s1.grade>s2.grade; end)
    
    for k,v in pairs(student) do
    	print(v.name .. "   " .. v.grade)
    end
    
    --[[
    b   100
    a   80
    c   60
    ]]
    

    table.getn()

    tab1 = {a="b",11,nil,22,b="c"}
    
    --getn等价于#
    --只计算非键值对的个数
    print(table.getn(tab1))
    print(#tab1)
    
    --[[
    3
    3
    ]]
    

    table.concat()

    fruits = {"banana","orange","apple"}
    -- 返回 table 连接后的字符串
    print("连接后的字符串 ",table.concat(fruits))
    
    -- 指定连接字符
    print("连接后的字符串 ",table.concat(fruits,", "))
    
    -- 指定索引来连接 table
    print("连接后的字符串 ",table.concat(fruits,", ", 2,3))
    
    --[[
    bananaorangeapple
    banana, orange, apple
    orange, apple
    ]]
    

    2、table实现面向对象

    之前写的博客
    https://www.cnblogs.com/Fflyqaq/p/13292388.html

  • 相关阅读:
    高性能服务器开发基础系列 (二)Reactor模式
    高性能服务器开发基础系列 (二)Reactor模式
    高性能服务器开发基础系列 (二)Reactor模式
    javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    JS基础入门篇(十)— 数组方法
    JS基础入门篇(十)— 数组方法
    C# WinForm 文件上传下载
  • 原文地址:https://www.cnblogs.com/Fflyqaq/p/13345183.html
Copyright © 2011-2022 走看看