Kotlin Arrays

Although they are used less frequently than the collection class, Kotlin does have arrays. Kotlin arrays are declared as Array where T corresponds to the type of object the array holds. The array type is packed with factory methods and extension functions, all of which make working with arrays easy.

Creating Arrays

We normally use the arrayOf() method to create an array.

//Create an initialized array
val belchers = arrayOf("Bob", "Linda", "Tina", "Gene", "Louise")

//Create an empty array with five elements
val teddies = arrayOfNulls<String>(5)

//Create an array with a generator function
val morts = Array(5){ "mort" }

In the first example, we use arrayOf() and then pass any number of arguments to the function. The Kotlin compiler will determine the type of object based on the arguments and create an array initialized with arguments. The second example creates an object array of the specified size and initialized with nulls. There are overloaded versions for primitives. The final function creates an array with a specified number of elements and initializes it with a generator function.

Extension Functions

toList() and other conversion functions

True to Kotlin style, Arrays have methods that convert the array to a collection class. Here is an example of converting an array to an immutable list.

val belcherList = belchers.toList()

sort()

Arrays have a sort() method that makes it easy to sort an array. There is an overloaded version that takes a comparator object also.

belchers.sort()

binarySearch

Kotlin arrays have a binarySearch method that lets developers apply the binarySearch algorithm to the array. Note that the function will fail if the array isn’t sorted first.

belchers.sort()
belchers.binarySearch("Linda") //returns 2

Arrays Class

The Arrays class is part of JDK and provides additional methods that are useful for working with arrays.

toString()

Calling toString() on an array doesn’t yield the result that one might expect. That’s because an array does not override toString() and instead uses the implementation found in java.lang.Object. To pretty print an array, we need to use Arrays.toString().

Arrays.toString(belchers)

equals

Arrays also use the equals() implementation found in java.lang.Object. If we want to compare two arrays by their elements, we use Arrays.equals().

val pestos = arrayOf("Jimmy", "Jimmy JR", "Andy", "Ollie")
val isEqual = Arrays.equals(belchers, pestos) //returns false

Putting it Together

Below is a Kotlin program that demonstrates arrays.

import java.util.*

fun main(args : Array<String>){
    val belchers = arrayOf(
                    "Bob",
                    "Linda",
                    "Tina",
                    "Gene",
                    "Louise")

    //Pretty print arrays using Arrays.toString()
    println("Printing the array")
    println(Arrays.toString(belchers) + "\n")

    println("Converting to a list")
    val belcherList = belchers.toList()
    println("belcherList => " + belcherList + "\n")

    println("Sorting")
    Arrays.sort(belchers)
    println("Sorted belchers => " + Arrays.toString(belchers) + "\n")

    println("Binary Search")
    println("Linda found at index => " + belchers.binarySearch("Linda") + "\n")

    println("Equals")
    val pestos = arrayOf("Jimmy", "Jimmy JR", "Andy", "Ollie")
    println(Arrays.toString(belchers) + " is equal to " + Arrays.toString(pestos) + " => " + Arrays.equals(belchers, pestos) + "\n")

    println("Filling an empty array")
    val teddies = Array(5){ "teddy" }
    println("After filling teddies => " + Arrays.toString(teddies) + "\n")
}

Output

Printing the array
[Bob, Linda, Tina, Gene, Louise]

Converting to a list
belcherList => [Bob, Linda, Tina, Gene, Louise]

Sorting
Sorted belchers => [Bob, Gene, Linda, Louise, Tina]

Binary Search
Linda found at index => 2

Equals
[Bob, Gene, Linda, Louise, Tina] is equal to [Jimmy, Jimmy JR, Andy, Ollie] => false

Filling an empty array
After filling teddies => [teddy, teddy, teddy, teddy, teddy]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s