For more information, see Array data structure on Wikipedia.
Arrays visually represented. Numbers are indices.

An array is an ordered collection of elements with each element accessible by an index or multiple indices (like a coordinate) in the case of a multi-dimensional array. A 1-dimensional array is similar to a list, however, arrays are more specific and simpler in their implementation, being generally a portion of memory where every element is the same type and their locations are known directly from their indices.

The first element index is often either 0 or 1 and said to be using zero-based indexing or one-based indexing, respectively.

Uses

Arrays are versatile and have countless uses. 1-dimensional arrays in particular can be used to build other collections such as lists and strings.

Higher dimensions can be used to store:

Multidimensional Arrays in Scratch

Scratch does not support arrays however lists are functionally similar to 1-dimensional arrays. It is possible to pack multi-dimensional data into arrays of lower dimensions, thus allowing for one or more lists to be used.

Methodology

Any multidimensional array can flattened into a 1-dimensional array. The indices can be calculated knowing the size of the multidimensional array along each dimension.

  • For a 2D array indexed from 0, the index in the 1D array is ix+(iy*Sx), where ix and iy are the offsets along the x and y dimensions, and Sx is the size of the array along the x dimension.
  • For a 3D array indexed from 0, the index in the 1D array is ix+(iy*Sx)+(iz*Sx*Sy).

Addition of 1 is needed if the 1-dimensional array uses 1-based indexing, such as when represented by a Scratch list.

2D

The following shows how a 2D array can be implemented in Scratch using a list.

The list will be simply called "array" and two variables will be used to store its size: (size x) and (size y)

Initialization

The number of items in the list are equivalent to multiplying the sizes of the x and y dimensions. To initialize the array with empty values, clear the list and add all the items as needed.

delete all of [array v]
repeat ((size x) * (size y))
  add [] to [array v]
end

Index

To get the index of a list item, use:

set [i v] to ((((iy) * (size x)) + (ix)) + (1))

"ix" and "iy" must be ≥0 and less than the size of their corresponding dimension.

To go the other way and get the dimension offsets from index, use:

set [ix v] to (((i) - (1)) mod (size x))
set [iy v] to ([floor v] of (((i) - (1)) / (size x)))

"i" must be ≥0 and less than the number of items in the list.

Set Value

To set a value, use the above calculated index:

replace item ((((iy) * (size x)) + (ix)) + (1)) of [array v] with [value]

Get Value

Similarly, to get a value:

(item ((((iy) * (size x)) + (ix)) + (1)) of [array v])

3D

A 3D array is much the same as the 2D array, except the indices are calculated with the 3rd dimension.

To get the index of a list item, use:

set [i v] to (((((iz) * ((size x) * (size y))) + ((iy) * (size x))) + (ix)) + (1))

To go the other way and get the dimension offsets from index, use:

set [ix v] to (((i) - (1)) mod (size x))
set [iy v] to (([floor v] of (((i) - (1)) / (size x))) mod (size y))
set [iz v] to ([floor v] of (((i) - (1)) / ((size x) * (size y))))

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.