-- Array print('-----Array-----') Data = {} for i=1,5 do Data[i]=i*i -- Access by Index end table.insert(Data,100) -- Insert a Item table.remove(Data) -- Remove a Item for i=1,#Data do -- Get Length print(i..' = '..Data[i]) end -- 这里效率明显太低了 -- Queue print('-----Queue-----') Data = {} for i=1,5 do table.insert(Data, i) -- Push end while #Data > 0 do print('Front: '..Data[1]) -- Front table.remove(Data, 1) -- Pop end -- Stack print('-----Stack-----') Data = {} for i=1,5 do table.insert(Data, i) -- Push end while #Data > 0 do print('Top: '..Data[#Data]) -- Top table.remove(Data) -- Pop end -- Set print('-----Set-----') Data = {} Data['Apple'] = true if Data['Cat'] == nil then Data['Cat'] = true -- Insert to Set end Data['Apple'] = nil -- Erase from Set -- Map print('-----Map-----') Data = {} Data['Apple'] = 100 -- Add by Key & Value Data['Banana'] = 200 for key,value in pairs(Data) do print('key = '..key..', value = '..value) -- Access All Key & Value end print('Apple = '..Data.Apple..' & '..Data['Apple']) -- Access Value by Key print('Length of Map(or Set) = '..#Data) -- It doesnt work!
输出: -----Array----- 1 = 1 2 = 4 3 = 9 4 = 16 5 = 25 -----Queue----- Front: 1 Front: 2 Front: 3 Front: 4 Front: 5 -----Stack----- Top: 5 Top: 4 Top: 3 Top: 2 Top: 1 -----Set----- -----Map----- key = Banana, value = 200 key = Apple, value = 100 Apple = 100 & 100 Length of Map(or Set) = 0