Table is the most important and powerful weapon in the Lua language, and the simplest constructor is {}. -So when you see {}, you actually call a constructor.
Initialize the array directly:
Days = {"Monday","Tuesday","Wednesday "}
Initialize a table as a record:
Days = {m = "Monday", t = "Tuesday", w= "Wednesday"}
Note that the record is actually equivalent to M,T,W as the following table, so the default table days[0] returns nil
Add Field: You can add any type of field to the table, and the constructor {} only affects initialization.
0 0, label="console"= {sin (0), sin (1), sin (2 )}w[1"another field"= w
field is a key value
Construct a linked list with table:
Nil for inch Io.lines Do = {next = List,value = line} end = while 1 do Print (l.value) = L.Nextend
Record and list mashup:
polyline = {color = "blue", thickness = 2,npoints = 4,{x = 0, y = 0},{x = -10, y = 0},{x = -10, y = 1},{x= 0, y = 1}}print ( polyline[1].x)
List and record styles are exceptions to general initialization:
{x=0, y=0} <--> {["x"]=0,["Y"]=0}
{"red","green","blue" } <--> {[1] = "Red", [2] = "Green", [3] = "Blue"}
If you want the array to start with 0: (not recommended)
Days = {[0"Sunday","Monday","Tuesday ","Wednesday"}
Constructors are optional, convenient for later expansion/, and can be substituted for each other.
The construction of Lua language--table