# BlitzMax/Language/Arrays

Arrays are used to store sequences of variables, or elements. An element within an array is accessed by indexing the array with an integer offset.
The general syntax for indexing an array is:
       ArrayExpression [ index1 , index2 etc... ]

## Creating Arrays

The most common way to create an array is when declaring a variable:
This will initialize the int_array variable with a 10 element array. You can declare an 'empty' array by using:
An empty array is identical to an array with 0 elements.
Comment: Something to remember is that, unlike in earlier versions of Blitz Basic, arrays are now zero based. That is, the first element of the array has an index of 0. So if you declare an array with 4 elements, they will be indexed from 0 to 3.
Arrays may also be created 'on the fly' using the syntax:
       New [ Dimension1 , Dimension2 etc... ]
This returns an array of the specified dimension(s) with each element initialized to Null. For example:
'Auto arrays' may be created using the syntax:
       [ Element1 , Element2 etc... ]
This returns a 1 dimensional array containing the specified elements, for example:
Each element of an auto array must have exactly the same type. If necessary, you can use type conversions to enforce this.
Arrays also provide the following methods:
| Method | Description |
| Sort( ascending=True ) | Sort the array. |
| Dimensions:Int | Get array dimensions. |
Arrays also provide a read-only length field that returns the total number of elements in the array.
Here are some examples of using array methods:

# Arrays Tutorial

Arrays are really objects, so the following code does not copy the array. It only creates another variable that points to the same object.
To copy an array, you must use slices. The syntax for slicing is array[from..to].Both from and to may be omitted; they default to 0 and the last index, respectively. Copying the array is simple:
Resizing an array using slices:
Note that you cannot use slices with multi-dimensional arrays.

## Resizing an Array of Arrays

Note: this is a slightly advanced topic which may confuse beginners.
Although you cannot resize normal multi-dimensional arrays, you can resize an array of arrays (that being a 1D array where each element is a 1D array).
You first need to resize the main array and then loop through each element in it and resize the arrays that they contain.
Example:

## Multi-Dimensional Arrays

To create an array with more than one dimension, you can specify indexes separated by commas:
You can then refer to them like so:

## Multi-Dimensional Auto Arrays

Note: this is a slightly more advanced topic and as such, beginners may find it confusing.
It is not currently possible to create normal multi-dimensional arrays AND auto-initialize them. You can however cheat a little:
What the above actually does is create an array of type String Array. If you were to attempt to refer to it in the conventional manner it would cause an error (Incorrect number of array dimensions).

## Arrays in Types

If you wish to place an array in a type you must initialize them using New before you can use it. You cannot declare the size of the array on the line after Field. For now you actually can directly declare the size of a Field array. Could not do that for Type Globals though. ( So someone could perhaps sort out this article. Might do that myself should an opportunity rise. )
To make things easier for yourself you can create the array inside the type's New method.
You can also do this to multi-dimensional arrays. Just put commas between the brackets.

## Array Methods

Arrays provide a Sort method, a Dimensions method and a read-only length field. In addition, there are three internal methods which you may find of some use.
Takes an optional ascending parameter that defaults to true. Here is an example of sorting an array:
Returns an int array with array dimensions. So for example Int[10,5].Dimensions would return Int[2] equal to the auto array [10,5].
Returns an Int value indicating the number of dimensions in the array.
Byte Ptr to a value representing the data type of the array elements.
Type encoding will return one of the following: TYPE_BYTE, TYPE_SHORT, TYPE_INT, TYPE_FLOAT, TYPE_LONG, TYPE_DOUBLE, TYPE_STRING, TYPE_OBJECT, TYPE_ARRAY, TYPE_POINTER.
Returns an Int value, the number of elements * 4.
Example:

## Iterating through Array Elements

If you want to loop through each element of an array you can use a For-EachIn loop:
This is all very well, but what if you want to only loop through a portion of them? Say for instance, the last five. Well for that, you need a slice like so: