1. Overview

In this tutorial, we’ll see how to create an array of primitives and ordinary types. Additionally, we’ll show how to initialize an array of String objects.

2. Arrays for Primitive Types

Let’s first have a look at how Kotlin handles an array of primitive types. For that purpose, Kotlin provides ByteArray, ShortArray, IntArray, CharArray, DoubleArray, and FloatArray. Thanks to that, we don’t have to use the Array class. Moreover, we avoid boxing and unboxing. Let’s create a simple example:

@Test
fun `given int array when checked values are correct`(){
    val arrayOfPrimitives = IntArray(4)
    arrayOfPrimitives[0] = 1
    arrayOfPrimitives[1] = 2
    arrayOfPrimitives[2] = 4
    assertThat(arrayOfPrimitives).containsExactly(1, 2, 4, 0)
}

We created an array of size four and initialized it with values. After that, we checked all values. The last element is not set in the code. It is initialized with zero, which is a default value.

3. Array for Ordinary Type

Let’s now look at how to create an array of an ordinary type. For that purpose, there is an Array class. It creates an array of objects of a given type. Moreover, the most popular way of creating an array is the arrayOf() function:

@Test
fun `given Integer array when checked values are correct`(){
    val arrayOfInteger = arrayOf(1,2,3)

    assertThat(arrayOfInteger)
      .hasOnlyElementsOfTypes(Integer::class.java)
      .hasSize(3)
      .containsExactly(1,2,3)
}

We created an array of Integers. The arrayOf() method uses autoboxing to create Integer objects from primitive ones. Moreover, we check that the created array contains three Integers.

4. Creating String Array

After that, let’s show how to initialize an array of String objectsThe String type is an ordinary type. Because of that, we have to use the Array type for the array creation.

The first and most common way to do it is the arrayOf() method previously introduced. To create an empty array, we call it only with the type specified:

@Test
fun `given empty String array when checked there are no values`() {
    val arrayOfString = arrayOf<String>()

    assertThat(arrayOfString)
      .hasOnlyElementsOfTypes(String::class.java)
      .isEmpty()
}

Additionally, we may initialize the array with nulls:

@Test
fun `given String array with nulls when checked values are correct`(){
    val arrayOfString = arrayOfNulls<String>(3)

    assertThat(arrayOfString)
      .hasSize(3)
      .containsOnlyNulls()
}

When we know values, we can provide them as method arguments:

@Test
fun `given String array when checked values are correct`(){
    val arrayOfString = arrayOf("black", "white", "orange")

    assertThat(arrayOfString)
      .hasOnlyElementsOfTypes(String::class.java)
      .hasSize(3)
      .containsExactly("black", "white", "orange")
}

In addition, we can initialize the array with all identical values:

@Test
fun `given String array with same values when checked values are correct`() {
    val arrayOfString = Array<String?>(3){"initial value"}

    assertThat(arrayOfString)
      .hasOnlyElementsOfTypes(String::class.java)
      .hasSize(3)
      .containsOnly("initial value")
}

Moreover, we may access the element number in the iterator:

@Test
fun `given String array initialized with element no when checked values are correct`() {
    val arrayOfString = Array<String?>(3){"element no = $it" }

    assertThat(arrayOfString)
      .hasOnlyElementsOfTypes(String::class.java)
      .hasSize(3)
      .containsOnly("element no = 0", "element no = 1", "element no = 2")
}

As shown above, there are many ways of initializing the String array. Its construction is very flexible.

5. Conclusion

In this short article, we saw how to create an array of primitives and ordinary types. Moreover, we showed many ways of creating the String array.

As always, the source code of the examples is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.