Similar to other programming languages, arrays in Kotlin provide the ability to store and manage an ordered collection of elements of the same data type, such as a list of numbers, characters, or objects. They enable structured and organized storage of data as well as efficient access to individual elements via an index. Arrays are used in many use cases, from simple lists to tables to more complex multidimensional data structures. In Kotlin, arrays are objects that have the specially designed data type Array have.

The main properties of arrays in Kotlin are:

Same data type of included elements: This means that all objects within an array must have the same data type. For example just Int, String or Double etc., for example a combination of Int and String would lead to a program error.

In order to specify the data type for the elements within an array, the type of objects contained is specified in angle brackets after the array type when declaring an array. The following example creates an array of Int-Values ​​created:

The next step is to use the built-in function arrayOf() a row of Int-passing values ​​to array:

Indexed items: The elements in an array are indexed, meaning they have a specific position within the array that can be accessed via the associated index. Indexing of arrays starts at 0, i.e. the first element has the index 0, the second element the index 1, the third the index 2 and so forth. The index is given in square brackets. Let's look at our previous example:

Fixed size: Arrays have a fixed size, which is set when they are created. Once created, the size of an array cannot be changed. In the example below we are trying an additional element with index 5 add to the array:

To create an array in Kotlin you can also use the Constructor of class Array use which takes two parameters. The first parameter specifies the size of the array while the second parameter specifies the Initialization lambda contains. The initialization lambda is an expression that is executed for each element of the array and calculates the initial value for that element.

The Initialization lambda can also contain more complex expressions and therefore carry out more sophisticated calculations.

Iterating over arrays in Kotlin

In Kotlin, you can iterate through arrays and access their elements in various ways. One possibility is to have one for loop to use. Example:

Another possibility is as part of a for loop to use the index as a running variable to iterate through the elements of an array one after the other.

A slim alternative is to use the forEach() function, which performs an action on each element in the array.

The function forEach lists the specified lambda expression (lambda function) for each element of the array, passing each element as an argument to the lambda expression. The word it in the Lambda function represents the current element to which the action is applied within the function.

Comment: The key word it In Kotlin, stands for an implicit name of the individual parameter in a Lambda function. If a Lambda function has a single parameter, the parameter name can be omitted and the implicit one instead it  should be used.

Check if an element exists in an array

Let's assume we have the task of checking an array for the presence of the name “Jerry” to check. We have two options for this. Option number 1 is to use the in operators:

It is also possible to use a loop to iterate over all the elements of the array one by one and check whether each element corresponds to the element you are looking for. Example:

Special functions for initializing arrays for primitive data types

In Kotlin, there is a special array type for each primitive data type. This includes BooleanArray, ByteArray, ShortArray, IntArray, LongArray, CharArray, FloatArray and DoubleArray. These special array types allow the creation of arrays for the respective data type, making data handling and processing easier.

Initialization of these arrays can be done either by explicitly assigning values, as in the examples above, or by using constructors defined for the specific array types.

Multidimensional arrays

Previously we only looked at one-dimensional arrays, which can be thought of as a sequence or row of values. 

However, there are several use cases in software development that require the representation and processing of more complex, multi-dimensional data structures. Some of the most common and well-known usage scenarios for multidimensional data organization are: mapping tables and matrices with numbers to perform calculations or representing pixel values ​​in images as well as volume values ​​in 3D models. To store and process such data structures in Kotlin multidimensional arrays .

Multidimensional arrays in Kotlin are data structures consisting of nested arrays that organize data into multiple dimensions or levels. They can be thought of as arrays of arrays.

For example, a two-dimensional array (2D array) is like a table in which data is arranged in rows and columns.

It is also possible to loop through two-dimensional arrays using two nested loops.

A three-dimensional array (3D array) is like a stack of tables on top of each other and can be created by nesting an array of 2D arrays. 

In the examples above, 2D and 3D arrays have been created and filled with values. Note that these examples use fixed-size arrays, but you can also use ArrayLists to create dynamic, multidimensional arrays. We will look at this topic in detail later in the tutorial.

Leave a Comment

Your e-mail address will not be published. Required fields are marked with * marked