NetLogo Array Extension

Using

The array extension is pre-installed in NetLogo.

To use the array extension in your model, add a line to the top of your Code tab:

extensions [array]

If your model already uses other extensions, then it already has an extensions line in it, so just add array to the list.

For more information on using NetLogo extensions, see the Extensions Guide

When to Use

In general, anything you can do with an array in NetLogo, you could also just use a list for. But you may want to consider using an array instead for speed reasons. Lists and arrays have different performance characteristics, so you may be able to make your model run faster by selecting the appropriate data structure.

Arrays are useful when you need a collection of values whose size is fixed. You can quickly access or alter any item in an array if you know its position.

Unlike NetLogo’s lists and strings, arrays are “mutable”. That means that you can actually modify them directly, rather than constructing an altered copy as with lists. If the array is used in more than one place in your code, any changes you make will show up everywhere. It’s tricky to write code involving mutable structures and it’s easy to make subtle errors or get surprising results, so we suggest sticking with lists and strings unless you’re certain you want and need mutability.

Example use of Array Extension

Primitives

array:from-list array:item array:set array:length array:to-list

array:from-list

array:from-list list

Reports a new array containing the same items in the same order as the input list.

array:item

array:item array index

Reports the item in the given array with the given index (ranging from zero to the length of the array minus one).

array:set

array:set array index value

Sets the item in the given array with the given index (ranging from zero to the length of the array minus one) to the given value.

Note that unlike the replace-item primitive for lists, a new array is not created. The given array is actually modified.

array:length

array:length array

Reports the length of the given array, that is, the number of items in the array.

array:to-list

array:to-list array

Reports a new list containing the same items in the same order as the given array.