lua配列とチェーンテーブル

750 ワード

--[[
       
    :             0
]]

--   5*6     
mt = {}
for i = 1, 5 do
	mt[i] = {}
	for j = 1, 6 do
		mt[i][j] = 0
	end
end

--   5*6     
mt1 = {}
for i = 1, 5 do
	for j = 1, 6 do
		mt1[i * 6 + j] = 0
	end
end


print(table.getn(mt))
print(table.getn(mt1)) -- Output:36,       5 * 6 + 6


--[[
  
]]
-- table         ,     
l1 = {1, 2, 3}
l2 = l1
l2[2] = 5
print(l1[2])

l1[1] = nil
print(l2[1])


--       
function initSingleList(t)
	list = nil

	for _, v in ipairs(t) do
		--      list          ,  list next    list
		--   list      ,next value
		list = {next = list, value = v}
	end

	return list
end

t = {1, 2, 3, 4, 5}
list = initSingleList(t)

--        
l = list
while l do
	print(l.value)
	l = l.next
end