Skip to content

Arrays, Lists

Kameron Brooks edited this page Dec 26, 2018 · 3 revisions

ILists

Anything that implements the IList interface can be used like an array. This includes Arrays, Lists and ArrayLists. Anything in this documentation that applies to an Array will also apply to any implementer of IList.

Element Access

Elements in an array can be accessed in several ways.

Integer

The most common way is to accessed with an integer index. This is the way you are familiar with if you are familiar with c/c#.

arr[0];   // Get element at index 0 from array

This operation is read or write, the element can be read from and assigned to.


Array

You can also access elements with another array of indices to get or set multiple values in an array

arr [ [0,1,2] ];  // This will return a new array that contains the elements at index 0, 1, and 2  in the original array

arr [ [0,1,2] ] = 10 // This will set elements 0,1,2 in arr to the value 10;

This operation is read or write, the element can be read from and assigned to.

I will show more interesting uses for this feature later in this page.


.last

You can get the last index of an array by using the .last keyword inside the square brackets

arr[.last];      // Returns the last element of the array
arr[.last - 1];  // Returns the second to last element of the array

Interp Operator

You can use the interp operator .. to return a list of integers between 2 integers (including the first and last integers)

0..99;   // Returns an integer array that contains integers 0 through 99   [0,1,2,3,...98,99]
99..0;   // Returns an integer array that contains integers 99 though 0    [99,98,97,...,1,0]
-50..50; // Returns an integer array that contains integers -50 though 50
n..m;    // Returns an integer array that contains integers n though m

Putting it All Together

Copying an array

int[] arr2 = arr[0..(.last)];

Getting the first half of an array:

int[] arr2 = arr[0..(.last/2)];

Getting the second half of an array:

int[] arr2 = arr[(.last/2 + 1)..(.last)];

Setting the second half of an array to 0:

arr[(.last/2 + 1)..(.last)] = 0;

Reversing an array

int[] arr2 = arr[.last..0];

Getting the first 2 and last 2 elements in an array

int[] arr2 = arr[[0, 1, .last-1, .last]];

Setting the first 2 and last 2 elements in an array to 1

arr[[0, 1, .last-1, .last]] = 1;

Multidimensional Access Syntactic Sugar

You can access 1D arrays as if they are multidimensional arrays by using the multidimensional array access syntax. The multidimensional array accessor is great for working with grids, matrices

arr[x:w, y:h];   // This is the same as arr[x + y * w]
arr[x:w, y:h, z:d];   // This is the same as arr[x + y * w + z * h * w]

This syntax will work for an arbitrary amount of dimensions. Each dimension (except for the last) will need to provide its size. For example, we will use a chessboard. Chess boards are 8 by 8 grids. 64 possible tiles.

object[64] tiles;

tile[0:8, 0:8] = null; // Set tile at column 0 of 8 row 0 of 8 to null
tile[1:8, 0:8] = null; // Set tile at column 1 of 8 row 0 of 8 to null
tile[1:8, 2] = null; // Set tile at column 1 of 8 row 2 to null, last size can be omitted