We can use arrays to store a bunch of data of the same type, and String is no exception. In this topic, we will take a close look at string arrays and cover the essential basics from initializing an array to changing its contents. Note, you can do the same with any other types.
Initialization
Kotlin does not provide a special function to create an array of strings, so you need to use the function arrayOf which is already familiar to you. Take a look:
val stringArray = arrayOf("array", "of", "strings")
Basically, the function arrayOf is universal: you can use it to collect any type of data in an array, including String.
If you want to achieve strict behavior by explicitly defining the type, you can do it like this:
val stringArray = arrayOf<String>("array", "of", "strings")
You can also initialize an empty array to be filled with strings later. For this, you need the function emptyArray:
val newEmptyArray = emptyArray<String>()
Note that if you choose to use this function, you have to define the type of elements.
The same actually applies to other types: an array created with emptyArray() can be filled with elements of any type that is defined.
Accessing elements
Since everything you already know about arrays still applies, you can access a certain element by its index:
val stringArray = arrayOf("sagacity", "and", "bravery")
print(stringArray[2]) // bravery
Remember that indexing starts with . The index of the last element will equal .
You can set an array element in the same way:
stringArray[0] = "smart"
print(stringArray[0]) // smartOutputting an array
Now you know how to create and fill a string array and access its elements.
To see what we get as a result, and, for example, print a string array, use the familiar function joinToString():
val southernCross = arrayOf("Acrux", "Gacrux", "Imai", "Mimosa")
println(southernCross.joinToString()) // Acrux, Gacrux, Imai, Mimosa
Keep it in mind that joinToString() processes a single string of elements listed orderly and separated by a comma.
Working with multiple arrays
Let’s look at a couple of things you might want to know about working with several string arrays.
String arrays can be added
You can concatenate several arrays as shown in the following example:
val southernCross = arrayOf("Acrux", "Gacrux", "Imai", "Mimosa")
val stars = arrayOf("Ginan", "Mu Crucis")
val newArray = southernCross + stars
println(newArray.joinToString()) // Acrux, Gacrux, Imai, Mimosa, Ginan, Mu Crucis
String arrays can be compared
You cannot use the operators == and != to compare arrays because they simply do not compare their contents. With arrays, use the function contentEquals() instead:
val firstArray = arrayOf("result", "is", "true")
val secondArray = arrayOf("result", "is", "true")
val thirdArray = arrayOf("result")
println(firstArray.contentEquals(secondArray)) // true
println(firstArray.contentEquals(thirdArray)) // false
Note that it returns true only if the elements of the two arrays match completely and are arranged in the same order.
Changing the array contents
No matter if you're using val or var, you can edit the values of the existing elements through their index:
val southernCross = arrayOf("Acrux", "Gacrux", "Imai", "Mimosa")
var stars = arrayOf("Ginan", "Mu Crucis")
southernCross[1] = "star"
stars[1] = "star"
println(southernCross[1]) // star
println(stars[1]) // star
However, there is a great difference between val and var when it comes to reassignment. When you have a var array, you can change it by adding new elements to it.
Suppose we created an empty string array:
var southernCross = emptyArray<String>()
What good is an empty array? Let's fill it:
southernCross += "Acrux"
southernCross += "Gacrux"
southernCross += "Imai"
println(southernCross.joinToString()) // Acrux, Gacrux, Imai
Even if your array is not empty to begin with, you can still add elements in the same way:
var southernCross = arrayOf("Acrux", "Gacrux", "Imai")
southernCross += "Mimosa"
println(southernCross.joinToString()) // Acrux, Gacrux, Imai, Mimosa
Just like that, we’ve changed the content of the array by adding new elements. However, there’s one very important point to be made.
In Kotlin, the arrays are in a way unchangeable. Even if the array is declared with var, it cannot really be edited. In both examples, the array southernCross was actually re-created. In fact, we literally deleted the array and created another one instead.
So, we can add new elements if the array is declared as var. If you're using val, that is not possible:
val southernCross = arrayOf("Acrux", "Gacrux", "Imai", "Mimosa")
southernCross += "Ginan" // will not compileConclusion
Let's sum things up! You figured out how to use some familiar functions and techniques for working with string arrays.
You can:
Use
arrayOf()for initialization;Access strings in the array through their indexes;
Use
joinToString()to create a single string from the array and output it;Use
contentEquals()to compare two arrays;Add new elements to an array; keep in mind that you actually create it again instead of merely editing it.
This is a lot of material! Good news for you: you can work with arrays of any type in the same way. Good luck!