006 动态数组(lua)-实际应用

由于Lua的表会自动进行扩展,我们不需要像在Java中那样显式地管理数组的容量。
在Lua中,我们通常使用ipairs来遍历数组部分(即,具有连续整数键的部分)的表。
由于Lua的表本身就是动态的,我们不需要像Java中那样处理数组的扩容。


Array = {}  
Array.__index = Array  
  
function Array.new()  
    local self = {}  
    self.size = 0  
    self.data = {}  
    setmetatable(self, Array)  
    return self  
end  
  
function Array:getSize()  
    return self.size  
end  
  
function Array:isEmpty()  
    return self.size == 0  
end  
  
function Array:addLast(e)  
    table.insert(self.data, e)  
    self.size = self.size + 1  
end  
  
function Array:addFirst(e)  
    table.insert(self.data, 1, e)  
    self.size = self.size + 1  
end  
  
function Array:add(index, e)  
    if index < 1 or index > self.size + 1 then  
        error("Add failed. Require index >= 1 and index <= size + 1.")  
    end  
    table.insert(self.data, index, e)  
    self.size = self.size + 1  
end  
  
function Array:get(index)  
    if index < 1 or index > self.size then  
        error("Get failed. Index is illegal.")  
    end  
    return self.data[index]  
end  
  
function Array:set(index, e)  
    if index < 1 or index > self.size then  
        error("Set failed. Index is illegal.")  
    end  
    self.data[index] = e  
end  
  
function Array:contains(e)  
    for _, v in ipairs(self.data) do  
        if v == e then  
            return true  
        end  
    end  
    return false  
end  
  
function Array:find(e)  
    for i, v in ipairs(self.data) do  
        if v == e then  
            return i  
        end  
    end  
    return -1  
end  
  
function Array:remove(index)  
    if index < 1 or index > self.size then  
        error("Remove failed. Index is illegal.")  
    end  
    local ret = table.remove(self.data, index)  
    self.size = self.size - 1  
    return ret  
end  
  
function Array:removeFirst()  
    return self:remove(1)  
end  
  
function Array:removeLast()  
    return self:remove(self.size)  
end  
  
function Array:removeElement(e)  
    local index = self:find(e)  
    if index ~= -1 then  
        self:remove(index)  
    end  
end  
  
function Array:toString()  
    local res = "Array: size = " .. self.size .. "\n["  
    for i, v in ipairs(self.data) do  
        res = res .. v  
        if i ~= self.size then  
            res = res .. ", "  
        end  
    end  
    res = res .. "]"  
    return res  
end  
  
-- Example usage:  
local arr = Array.new()  
arr:add(1, 10)  
arr:add(2, 20)  
arr:add(3, 30)  
print(arr:toString())  -- Output: Array: size = 3 [10, 20, 30]

Array = {}  
Array.__index = Array  
  
function Array.new(capacity)  
    local self = {}  
    setmetatable(self, Array)  
  
    -- Initialize the array-like table  
    self.data = {}  
    self.size = 0  
    self.capacity = capacity or 10  
  
    return self  
end  
  
function Array:getSize()  
    return self.size  
end  
  
function Array:getCapacity()  
    return self.capacity  
end  
  
function Array:isEmpty()  
    return self.size == 0  
end  
  
function Array:addLast(element)  
    self:add(self.size + 1, element)  
end  
  
function Array:addFirst(element)  
    table.insert(self.data, 1, element)  
    self.size = self.size + 1  
    -- Handle capacity increase if needed (omitted for simplicity)  
end  
  
function Array:add(index, element)  
    if index < 1 or index > self.size + 1 then  
        error("Index out of bounds")  
    end  
  
    table.insert(self.data, index, element)  
    self.size = self.size + 1  
  
    -- Handle capacity increase if needed (omitted for simplicity)  
end  
  
function Array:get(index)  
    if index < 1 or index > self.size then  
        error("Index out of bounds")  
    end  
  
    return self.data[index]  
end  
  
function Array:set(index, element)  
    if index < 1 or index > self.size then  
        error("Index out of bounds")  
    end  
  
    self.data[index] = element  
end  
  
function Array:remove(index)  
    if index < 1 or index > self.size then  
        error("Index out of bounds")  
    end  
  
    local element = table.remove(self.data, index)  
    self.size = self.size - 1  
    return element  
end  
  
function Array:removeFirst()  
    return self:remove(1)  
end  
  
function Array:removeLast()  
    return self:remove(self.size)  
end  
  
function Array:toString()  
    local str = "Array: size = " .. self.size .. ", capacity = " .. self.capacity .. "\n["  
    for i = 1, self.size do  
        str = str .. tostring(self.data[i])  
        if i ~= self.size then  
            str = str .. ", "  
        end  
    end  
    str = str .. "]"  
    return str  
end  
  
-- Usage example  
local arr = Array.new()  
arr:addLast(10)  
arr:addLast(20)  
arr:addFirst(5)  
print(arr:toString())
上一篇:EasyExcel数据导入


下一篇:cpu,缓存,辅存,主存之间的关系及特点