Kotlin

Kotlin Arrays

Kotlin provides a way to store multiple values in a single variable name. We can access them by an index specified, and the most fundamental one is through arrays. In the article, we will discuss Arrays in the kotlin language. Many data types, variables, and other packages are utilized in the Kotlin programming language. Among all, the Array is the most significant package for producing, storing, and retrieving data.

What are Arrays in Kotlin in Ubuntu 20.04?

Kotlin arrays are the same as the Java fundamental array type. Kotlin arrays are the same type as the normal variables and constants and carry various value elements in a single part of memory. Arrays in Kotlin are mutable by default and have fixed sizes. Mutable arrays in kotlin support both the read and write operation. Once we have defined an array in kotlin, we can also modify our elements in the future.

How to use the Kotlin Arrays in Ubuntu 20.04?

To understand the basics of using the arrays in Kotlin language, you should take a look at the following examples:

Example # 1: Creating ArrayOf() and ArrayOf<Int> Function in Kotlin in Ubuntu 20.04:

We can create an array with the Kotlin library that has built-in functions. The function is known as the arrayOf() function in kotlin. We can specify the value of elements to the arrayOf() function. Let’s use the arrayOf() function in code to understand how it works.

In the code implementation, we have used a “fun” keyword which is used for function declaration. In the function, we are going to perform code implementation. Here, we have declared the main() function used to execute the code. We have created a variable as the “val” keyword, which is defined as “myArray1”. For the “myArray1” variable, we have declared an array by using the arrayOf() function. The ArrayOf() function has stored five different numeric values. We can fetch these array numeric values through the for loop() function, which will iterate the array. In the for loop() function, we have set an interval between 0 to the size-1 of the array. The for loop() function is used “in” the keyword here. Then in the println() function, we have kept the variable “i” in the overloaded operator “[]” which will print the array element till the size-1 of the specified array. Also, we have a second array which is defined in the variable “myArray2”. The “myArray2” variable is initialized with the string values, which is also passed in for loop() function as “myArray1”.

The output of the above implementation is shown on the terminal screen of Ubuntu 20.04.

As above, we have an implicit type declaration of an array. We have an example code similar to the above code, but we are explicitly declaring an array by the arrayOf<ataType> function. Below we have used Int and string data types with the arrayOf() function.

The output of the explicit data type declaration is in the below image.

Example # 2: Creating Constructor for an Array in Kotlin in Ubuntu 20.04:

To generate an array, we can also create the constructor of an Array. The constructor requires two parameters to be passed, the size of an array and a function that accepts the particular index of the specified Elements. We will have the initial values of the element from the given array as a return value.

The main() function’s declaration is done with the “fun” keyword in the above code. Here, we have created a variable as “val” and defined the “val” as “Array_Element.” The “Array_Element” has been initialized with the array constructor. The constructor array has the size and has passed as a parameter. The size is set to value “10” for an array. Then, we have defined the expression “(i -> i * 2)” and initialized the value elements from 0 to 10. Now, the resultant array will be printed to the console screen through the println() function, which we have defined in the for loop() function.

The resultant output array is printed on the screen below.

Example # 3: Creating get() and set() function Array in Kotlin in Ubuntu 20.04:

As an array in kotlin is simply a class. As a result, we can use a class object’s member function to access its data. In the Kotlin array, we have get() and set() functions that are referred to as member functions. The get() function takes only one parameter, which is the element’s index. It returns the element value of the specified Index. The set() member function set() passed two parameter values; the index of the element and the value that is to be set.

In the example code, we have defined the main function. The main() function has a variable represented as “val” and defined as “Animal_names.” The “Animal_names” is initialized with the array having different kinds of Animal_names. We have used the arrayOf() function for array declaration in the code. Next, we have called member function set(). The set() member function takes two parameters, the index value as “1” and a value “panda” that is going to be placed on the index value 1. Through the set() function, we can modify the currently given array of strings. Also, we have set the index value “2” as “kangroo.” Now, access the modified array by calling the get() function and passing it to the println() function. The get() function will give the index value of 1,2, and 4 as we have passed these values in the get function.

We accessed and modified the array as the output in the below image.

Example # 4: Creating Traverse array in Kotlin in Ubuntu 20.04:

An array’s main feature is that it can be traversed programmatically. In an array, each element may be separately modified. Here, we have a technique to traverse an array by using a for loop function.

In the code above, we have an implementation of the main() function. This main() function will execute the traversed array. Firstly, we have defined “Numeric_Array” as a variable in the main() function, which is represented as “val.” The “Numeric_Array” is initialized as the array of integers by using the arrayOf function. After that, we have used the set() function, which modifies the index value of “3” and “5” to “12” and “100”. Then, we have a for loop function which traverses the array as we have passed Numeric_Array.indices. That means it will iterate over each index. By the println() function, we can fetch the traverse array.

We have got the output array shown on the terminal screen upon execution.

Conclusion:

The goal of the article was to show readers how to use arrays in Kotlin on Ubuntu 20.04. As arrays are the most fundamental part of any programming language. We started with a basic introduction to arrays, then moved on to the different examples and created arrays in different ways. These examples will help you to deal with arrays in the Kotlin programming language.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.