array_Tips.lua 585 B

12345678910111213141516171819202122232425262728293031323334
  1. ---@class array
  2. array = {}
  3. function array.clear(t)
  4. local count = #t
  5. for i = 1, count do
  6. table.remove(t)
  7. end
  8. end
  9. function array.length(t)
  10. return t and #t or 0
  11. end
  12. function array.contains(t, item)
  13. return array.indexOf(t, item) ~= nil
  14. end
  15. function array.indexOf(t, item)
  16. for k, v in ipairs(t) do
  17. if v == item then
  18. return k
  19. end
  20. end
  21. return nil
  22. end
  23. function array.random(t)
  24. math.randomseed(os.time())
  25. for i = 1, #t do
  26. local j = math.random(i)
  27. table[i], table[j] = table[j], table[i]
  28. end
  29. end