Skip to content

Lists

NBT supports the basic data type of lists, which MCFPP abstracts and provides as a separate type.

Basic Usage

Use the list<type T> type identifier to define a list, where the type parameter T represents the type of elements stored in the list.

mcfpp
list<int> l = []; # Create an empty list
l.add(1); # Add an element to the list
l.add(2); 
l.add(3); 
print(l); # Outputs [1,2,3]
print(l[0]);    # Outputs 1

l.removeAt(1); # Remove the element at index 1

Passing a list is by value, not by reference. In fact, all NBT-based data types are passed by value.

mcfpp
list<int> l = [1,2,3];
list<int> l2 = l;
l2.add(4);
print(l); # Outputs [1,2,3]
print(l2); # Outputs [1,2,3,4]

List Operations

The MCFPP standard library provides a series of functions for list operations.

Function NameParametersReturn ValueDescription
addT elementvoidAdds an element to the list
addAlllist<T> elementsvoidAdds a group of elements to the list
insertint index, T elementvoidInserts an element at a specified position
removeAtint indexvoidRemoves the element at the specified position
indexOfT elementintReturns the index of the specified element
lastIndexOfT elementintReturns the last index of the specified element
containsT elementboolChecks if the list contains the specified element
clearvoidvoidClears the list

Iterating Over Lists Future Feature

Use the foreach loop to easily iterate over all elements in a list.

mcfpp
list<int> l = [1,2,3];
foreach(i in l){
    print(i);
}