docs
Language Structure
Arrays

Intro

An array is a special variable, which can hold more than one value:

Ex

reka names  = ["Bwenge", "Nyabihu", ]

Why Use Arrays?

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

reka car1 = "Saab"
reka car2 = "Volvo"
reka car3 = "BMW"

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

The solution is an array!

An array can hold many values under a single name, and you can access the values by referring to an index number.

Creating an Array

Using an array literal is the easiest way to create a Kin Array.

Syntax:

ntahinduka array_name = [item1, item2, ...]

Spaces and line breaks are not important. A declaration can span multiple lines:

ntahinduka cars = [
  "Saab",
  "Volvo",
  "BMW"
];

You can also create an array, and then provide the elements:

ntahinduka cars = [];
cars[0]= "Saab";
cars[1]= "Volvo";
cars[2]= "BMW";

Accessing Array Elements

You access an array element by referring to the index number:

ntahinduka cars = ["Saab", "Volvo", "BMW"];
reka car = cars[0];

Array indexes start with 0. [0] is the first element. [1] is the second element.

Changing an Array Element

This statement changes the value of the first element in cars:

reka cars = ["Saab", "Volvo", "BMW"];
cars[0] = "Opel";

Array Methods

Array Length

You can get the length of an array by using KIN_URUTONDE.ingano() method.

Ex:

reka ijambo = ["Hello", "World"]
tangaza_amakuru(KIN_URUTONDE.ingano(ijambo))

Output: 2

Array Include (ifite)

KIN_URUTONDE.ifite(list, value) is nibyo when any element equals value (numbers, strings, and booleans are compared by value). You can also write list.ifite(value).

reka ijambo = ["Hello", "World"]
tangaza_amakuru(KIN_URUTONDE.ifite(ijambo, "Hello"))
tangaza_amakuru(KIN_URUTONDE.ifite(ijambo, "World"))

Both calls print nibyo.

Has key (ifite_ikirango)

KIN_URUTONDE.ifite_ikirango(list, key) is true if the array/object has that key ("0", "1", … for arrays), not that value.

reka ijambo = ["Hello", "World"]
tangaza_amakuru(KIN_URUTONDE.ifite_ikirango(ijambo, "0"))

Output: nibyo

Push / pop (mutate in place)

  • KIN_URUTONDE.ongera_kumusozo(list, value) appends value in place and returns the new length.
  • KIN_URUTONDE.siba_kumusozo(list) removes the last entry in place and returns the new length (not the removed value).
reka cars = ["Saab", "Volvo"]
tangaza_amakuru(KIN_URUTONDE.ongera_kumusozo(cars, "BMW"))
tangaza_amakuru(KIN_URUTONDE.ingano(cars))

Both lines print 3. cars now has three items.

Prepend / drop first (new array)

  • KIN_URUTONDE.injiza_ahabanza(list, value) returns a new array with value at index 0. The original is unchanged.
  • KIN_URUTONDE.siba_ahabanza(list) returns a new array without index 0.

Array join

You join elements of an array to make one word by using KIN_URUTONDE.kora_ijambo() method. There is no separator.

Ex:

reka cars = ["Saab", "Volvo", "BMW"];
tangaza_amakuru(KIN_URUTONDE.kora_ijambo(cars))

Output: SaabVolvoBMW