编辑代码

-- JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
-- BV1RQ4y1t77H


--[[ 定义一个数组
names = {"Trump", "ikun", "马老师"}
names[4] = "哟西"

for i=1, 4 do
    print("names["..i.."]="..names[i])  --显示为 names[1]=Trump
end
]]

--[[ 二维数组
arr = {}
for i=1, 3 do
    arr[i] = {}
    for j=1, 2 do
        arr[i][j] = i * j
    end
end

for i=1, 3 do
    for j=1, 2 do
        print(arr[i][j])
    end
end
]]

--[[ map 
Famous = {name="Trump", weight=79, depart="USA"}
-- 通过下标方式访问
Famous["gender"] = "Man"
-- 通过点方式访问
Famous.office = "white house"
]]

--[[
a = "xxx"
b = 3
c = 5
arr = {
    ["famous_" .. a] = true,
    [b + c] = "Hello",
    ["hi"] = 123
}
print(arr.famous_xxx)
print(arr[8])
print(arr.hi)
]]

--[[ map的key不占数组的索引值
Famous = {"ikun", name="Trump", weight=79, "马老师", depart="USA", "256机", "ProMax"}
print(Famous[1])  --ikun
print(Famous[2])  --马老师
print(Famous[3])  --256机
print(Famous[4])  --ProMax
]]

--[[ 定义数组,map混合结构
famous = {
    {name="Trump", weight=79},
    {name="ikun", weight=30},
    {name="马老师", weight=69},
    {name="哟西", weight=35}
}

for i=1, 4 do
    print(famous[i].name.." : "..famous[i].weight)
end
]]