lua首页投稿(暂停使用,暂停投稿)我是程序员;您好程先生;叫我序员就好了

Lua table.maxn()

2016-08-04  本文已影响2757人  AlbertS
计数.jpg

前言#

今天来看一个关于table的一个简单的函数,我们知道Lua数组中的索引一般从1开始,那么数组的length就等于数组的最大索引,但是当数组下标不是连续时,我们要活的最大索引要怎么办,那就需要使用我们今天所讲的函数了。

内容#


table.maxn()##


Uasge##

-- 各种索引都存在
local tabLanguage = { 
    up ="Lua",
    "c",
    "c++",
    [100] = "end",
    realend = "realend",
    [-1] = "haha";
};

for k,v in pairs(tabLanguage) do
    print(k,v)
end
print("LUA>>>>>>the maxn of table tabLanguage :", table.maxn(tabLanguage), "\n")

-- 使用默认数字索引
local tabTest1 = { 
    "c",
    "c++",
    "php"
};

for k,v in pairs(tabTest1) do
    print(k,v)
end
print("LUA>>>>>>the maxn of table tabTest1 :", table.maxn(tabTest1), "\n")

-- 负数索引
local tabTest2 = { 
    [-1] = "c",
    [-100] = "c++",
    [-10] = "php",
};

for k,v in pairs(tabTest2) do
    print(k,v)
end
print("LUA>>>>>>the maxn of table tabTest2 :", table.maxn(tabTest2), "\n")

-- 非数字索引
local tabTest3 = { 
    first = "c",
    second = "c++",
    third = "php"
};

for k,v in pairs(tabTest3) do
    print(k,v)
end
print("LUA>>>>>>the maxn of table tabTest :", table.maxn(tabTest3), "\n")
table_maxn.png

总结#

上一篇 下一篇

猜你喜欢

热点阅读