Server Side Kotlin-Array

Server Side Kotlin-Array

DevNation's photo
·Dec 22, 2022·

3 min read

Table of contents


Arrays in Kotlin are used to store multiple values having the same data types in a group.

Arrays are represented by a pair of square brackets []. The values in an array are known as elements and their position is defined by index ( or indices). Each element in an array is separated by commas (,).

Array follows index-based access. The index is nothing but the position of the elements of an array. The index always starts with 0.

Kotlin has two types of Arrays:- Primitive Arrays and Object Arrays.

Primitive Arrays :-

Kotlin has wide support for primitive arrays. There are dedicated arrayOf methods for the following types: double, float, long, int, char, short, byte, and boolean. To represent arrays of primitive types, Kotlin provides a number of separate classes, one for each primitive type. For example, an array of values of type Int is called IntArray. For other types, Kotlin provides ByteArray, CharArray , BooleanArray, and so on. All of these types are compiled to regular Java primitive Array.

There are 3 Methods to create a Primitive Array type:-

I) Primitive Array Constructor with Arguments:-

The constructor of the type takes a size parameter and returns an array initialized with default values for the corresponding primitive type

 >>> val parray = IntArray(5)

II) Factory Function with multiple arguments:-

The factory function ( intArrayOf for IntArray and others) takes a variable number of values as arguments and creates an array taking those values.

>>> val parray = intArrayOf(0, 0, 0, 0, 0)

III) Primitive Array Constructor with size and lambda:-

The constructor takes a size and lambda is used to initialize each element.

>>> val parraysqr = IntArray(5) { i -> (i+3) * (i+3) *(i+3) } 
>>> println(parraysqr.joinToString())

There are 3 Methods to create an Object Array type:-

I) arrayOf() function:-

To create an Object array in Kotlin, we use the arrayOf() function and place the values in a comma-separated list inside it.

>>> val languages = arrayOf("javascript", "kotlin", "python", "ruby")  

//Optionally we can provide a data type as follows:

>>> val languages = arrayOf<String>("javascript", "kotlin", "python", "ruby")

II) arrayOfNulls() function for null elements:-

The arrayOfNulls() function can be used to create an array of a given size filled with null elements.

>>>  val nularr = arrayOfNulls<Int>(4)

III) Object Array Constructor with size and lambda:-

The Array constructor takes the size of the array and lambda, and initializes each array element by calling the lambda.

 >>> val letters = Array<String>(26) { i -> ('z' - i).toString() }

>>> println(letters.joinToString(""))

get() set() method:-

Kotlin provides get() and set() member functions to get and set the value at a particular index.

>>> val languages = arrayOf<String>("javascript", "kotlin", "python", "ruby") 

>>>  println( languages.get(3))
>>> languages.set(3, "php")


An array is a collection of the same data items stored under a single name. Array follows index-based access. The index always starts with 0. There are two types of Arrays in Kotlin and each type is having 3 ways to create Arrays.

(code tested on kotlin REPL Kotlin version 1.7.21 (JRE 11.0.17+8-post-Ubuntu-1ubuntu220.04))

Share this