Arrays and Lists
Often in programming you will encounter a situation, where you need not one of something, but multiple elements. For example, when creating a crafting table recipe, you need not one input, but up to 9. Still, all of these elements somehow “belong” together as a single grouping.
Depending on whom you ask, this grouping can have many names.
These names could include Lists
, Arrays
, Sets
, Collections
, Fields
, Vectors
.
Furthermore, some of these names carry specific meanings with them as to what these structures allow you to do with the data stored inside them.
In this page, we will look at the way Lists
and Arrays
can be used from within ZenCode scripts.
For the terminology, we will call the items inside a List or an Array as Elements
of that structure.
For example, in the array below, "A"
, "B"
and "C"
would be the elements of the array arr
.
Also, this guide will only cover linear Arrays. These are the arrays that you will most likely encounter when working with mods.
The other sort of arrays are rectangular Arrays, which take more than one index at a time, like below. They will be covered in another wiki page eventually, though some of their properties are not yet implemented properly.
The main difference between Lists and Arrays
The most important difference between Lists and Arrays is their size. Or rather, if they can shrink or expand in size.
Arrays are said to have a fixed size. That means that once you have created an Array, you cannot add additional elements to it. You can, however, replace an element inside the array with another element.
Lists on the other hand are said to have a variable size. That means you can add additional elements to a list if you need to.
So if a List’s size can be changed but an Array’s cannot, why not always use Lists?
That comes from the ways that fixed sizes also gives you the ensurance that nothing gets messed up by accident.
For example, what if you registered a crafting recipe but then added a new item to the List you used as inputs?
Would the recipe now need to heed that additional item or adhere to the List you gave it at the beginning?
Additionally, the Array’s type syntax might be easier to read for some, especially when it comes to nested structures.
For example, string[][]
might be easier to read and understand than List<List<string>>
for some.
Lists and Arrays can be converted to each other
You can convert between Lists and Arrays if you need to. For example, you could convert an Array to a List, add an additional element to the List and then convert it back to an Array. However, note that these operations will always create new objects and not modify the existing ones.
Below you can see such an example:
Creating a List or an Array
Lists can be created via their constructor.
Arrays on the other hand, have multiple different ways to create them:
The easiest way is to simply specify the elements inside []
and with commas separating them from each other.
For most cases that way is the way to go, as you don’t have to write any more code than necessary.
The Examples in most of this wiki will use that format when using arrays, like for adding recipes to the crafting table.
More sophisticated ways include using functions to specify which element belongs where in the array.
In the examples, you need to fill ELEMENT_TYPE
with the proper type, e.g. string
or IItemStack
.
See further below for working examples
Retrieving and setting an element
You can access elements in Lists and Arrays using the Getter and Setter syntax. Here, each element has a number starting from 0.
Doing something for each element in the List or Array
Many times you create Lists or arrays in order to shorten your code. For this you can use for-loops.
Getting information on the List or Array
At some times you may need to get the length of an array, or whether or not it is empty or contains a given element.
These methods exist on both Lists and Arrays.
Other methods existing in Arrays and Lists
There are many more methods existing on Arrays and Lists.
Not all of them are working yet and not all of them are available for both structures.
If you are interested in these additional methods, check out the stdlib Entries for them.
You can find them here: