Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Lua Arrays

Lua Arrays

An array is a collection of elements of the same data type arranged in a certain order; it can be one-dimensional or multidimensional.

In Lua, an array is not a specific data type, but a data structure used to store a group of values.

In fact, there is no dedicated array type in Lua; instead, a data structure called "table" is used to implement the functionality of arrays.

The index keys of Lua arrays can be represented by integers, and the size of an array is not fixed.

In Lua, index values start at 1, but you can also specify starting at 0.


One-dimensional array

A one-dimensional array is the simplest array, whose logical structure is a linear list.

Access array elements using indices:

Example

-- create an array

local

myArray

=

{

10

,

20

,

30

,

40

,

50

}

-- access array elements

print

(

myArray

[

1

]

)

 

-- output 10

print

(

myArray

[

3

]

)

 

-- output 30

The output of the above code is:

10

30

To calculate the length of an array (i.e., the number of elements in the array), you can use the # operator:

Example

local

myArray

=

{

10

,

20

,

30

,

40

,

50

}

-- calculate the array length

local

length

=

myArray

print

(

length

)

-- output 5

The output of the above code is:

5

A one-dimensional array can use a for loop to iterate over the elements in the array, as in the following example:

Example

-- create an array

local

myArray

=

{

10

,

20

,

30

,

40

,

50

}

-- loop through the array

for

i

=

1

,

myArray

do

   

print

(

myArray

[

i

]

)

end

The output of the above code is:

10

20

30

40

50

lua indices start at 1 by default:

Example

array

=

{

"Lua"

,

"Tutorial"

}

for

i

=

0

,

2

do

   

print

(

array

[

i

]

)

end

The output of the above code is:



nil

Lua

Tutorial

As you can see, we can use integer indices to access array elements; if the specified index has no value, it returns nil。

In addition, we can also use negative numbers as array index values:

Example

array

=

{

}

for

i

=

2

,

2

do

   array

[

i

]

=

i

2

end

for

i

=

2

,

2

do

   

print

(

array

[

i

]

)

end

The output of the above code is:



-4

-2

0

2

4

We can also modify elements in the array:

Example

-- create an array

local

myArray

=

{

10

,

20

,

30

,

40

,

50

}

-- modify array elements

myArray

[

2

]

=

25

-- loop through the array

for

i

=

1

,

myArray

do

   

print

(

myArray

[

i

]

)

end

The output of the above code is:



10

25

30

40

50

We can also add elements to the array:

Example

-- create an array

local

myArray

=

{

10

,

20

,

30

,

40

,

50

}

-- add a new element to the end of the array

myArray

[

myArray

1

]

=

60

-- loop through the array

for

i

=

1

,

myArray

do

   

print

(

myArray

[

i

]

)

end

The output of the above code is:

10

20

30

40

50

60

We can also delete elements from the array:

Example

-- create an array

local

myArray

=

{

10

,

20

,

30

,

40

,

50

}

-- delete the third element

table.remove

(

myArray

,

3

)

-- loop through the array

for

i

=

1

,

myArray

do

   

print

(

myArray

[

i

]

)

end

The output of the above code is:

10

20

40

50


Multidimensional Arrays

A multi-dimensional array is an array that contains arrays, or an index key of a one-dimensional array corresponds to an array.

The following is a three-row, three-column multi-dimensional array:

Example

-- initialize the array

array

=

{

}

for

i

=

1

,

3

do

   array

[

i

]

=

{

}

     

for

j

=

1

,

3

do

         array

[

i

]

[

j

]

=

i

j

     

end

end

-- access the array

for

i

=

1

,

3

do

   

for

j

=

1

,

3

do

     

print

(

array

[

i

]

[

j

]

)

   

end

end

The output of the above code is:



1

2

3

2

4

6

3

6

9

A three-row, three-column multi-dimensional array with different index keys:

Example

-- initialize the array

array

=

{

}

maxRows

=

3

maxColumns

=

3

for

row

=

1

,

maxRows

do

   

for

col

=

1

,

maxColumns

do

      array

[

row

maxColumns

col

]

=

row

col

   

end

end

-- access the array

for

row

=

1

,

maxRows

do

   

for

col

=

1

,

maxColumns

do

     

print

(

array

[

row

maxColumns

col

]

)

   

end

end

The output of the above code is:



1

2

3

2

4

6

3

6

9

As you can see, in the above example, the array is set with specified index values, which avoids nil values and helps save memory space.