So today we will be talking about arrays in Kotlin, as I’ve mentioned before there isn’t a special array type in Kotlin, arrays are a class like any other and the class is called array, in fact, the array class is a collections class.

there are two ways we can create an array class:

Elements are known while initialization

if you know the elements you want in the array in advance, the easiest way to do it is to use the arrayOf function:

 val names = arrayOf("John", "Jane", "Jill", "Joe")

Notice that we don’t have to specify the type. So if we wanted an array of Long, you’d either have to include the L suffix for all the numbers (otherwise the compiler will consider it as int array) or you’d have to specify the type:

val longs1 = arrayOf(1L, 2L, 3L)
val longs2 = arrayOf<Long>(1, 2, 3, 4)

Also, you can use the usual array indexing:

println(longs1[2])

you can also use the array constructor remember that, because Array is a class it’s going to have constructors and you can use the array constructor and pass a lambda expression to initialize the array, for example, if you wanted to initialize an array with even numbers between 0 and 30 inclusive:

18 is the number of elements and for each element take the index and multiply it by 2

val evenNumbers = Array(18) { i -> i * 2 }

this way is also good when you want to create an array of  1000 element

val bigOne = Array(1000) { i -> i + 1 }

which will loop from and add 1 to 1000 ( we added the +1 not to have 0 to 999).

also if we want to create an array full of zeros ( you can also go { i ->0 } but not needed)

val allZeroes = Array(100) { 0 }

Elements not known while initialization

In that case, the compiler won’t be able to infer the type, let’s say we want to create an int array:

 var someArray: Array<Int>
//then we can add the items later
 someArray = arrayOf(1, 2, 3, 4)

Now one of the cool features in Kotlin, we can actually mix data types!

val mixedArray = arrayOf("hello", 22, BigDecimal(10.5), 'a')

 

Difference between Array and primitive array

Now I hope you noticed that that mixed array is actually an array of any, I mean that’s the only way this is going to work, right?

But what about when we are going to use a Java method that expects an array of int?

like this, I will type it in the TestClass.java, notice that the Java method is expecting a primitive int array while in Kotlin arrays are actually collections:

//This is java 
public void printNumbers(int[] numbers) {
        for (int number: numbers) {
            System.out.println(number);
        }
    }

Then back to the kotlin class

val myIntArray = arrayOf(3, 9, 434, 2, 33)
TestClass().printNumbers(myIntArray)

Unfortunately, it will give type mismatch, which is the opposite of what happened with us in the previous lesson.

So this is one case in Kotlin, where you do have to do something different when we want to call something in Java. So if you know that you’re going to want to call Java with an array of primitive types, you should use the array subclass for the primitive type. There is int array, long array, char array, you get the idea.

Important!,  doing so is going to improve performance when the Kotlin application runs, because these special arrays that map to the primitive type are actually going to be treated as a primitive type arrays under the covers, so you actually get a performance boost by using them, so even if you’re not going to call a Java method that wants a primitive array, you can you can get a performance boost by using these special primitive array classes rather than just the general array class.

so to get rid of that error we have to do this

val myIntArray = intArrayOf(3, 9, 434, 2, 33)
TestClass().printNumbers(myIntArray)

Now another different other than performance is that, let’s say we create an int array of size 5, but not initialize it.

var someOtherArray = Array<Int>(5)
var someOtherArray2 = Array<Int>

The first one won’t work and will give an error because what we’re actually doing here is trying to call the array constructor and the array constructor wants two parameters, first the size of the array and second the list of values.

but when it comes to the primitive type arrays, you can do this sort of thing, let’s try the same thing :

 var someOtherArray = IntArray(5)

which will not give any error:),

But, how can you create an empty array that we know it’s size?

There is actually a way to do it without the need to use the primitive type array but I just didn’t like it, there is an empty array function but to be honest I am not clear on the use case for it because it really is an empty array you can’t assign anything to it. (Developers online where actually trying to find what’s the point of the empty array function is and nobody ever seems to have a good answer for that).

 

Converting between primitive and Any type array

we can do that easily

TestClass().printNumbers(evenNumbers.toIntArray())

which will work, and for the other way around using the toTypedArray method

val convertedIntArray = myIntArray.toTypedArray()

 

That’s it for arrays, I know there might be more to talk about but I think we covered the important points, See You Later:)