243

I see that Kotlin has ByteArray, ShortArray, IntArray, CharArray, DoubleArray, FloatArray, which are equivalent to byte[], short[], int[],char[], double[], float[] in Java.

Now I'm wondering, is there any StringArray equivalent to Java's String[]?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
chancyWu
  • 14,073
  • 11
  • 62
  • 81

8 Answers8

316

There's no special case for String, because String is an ordinary referential type on JVM, in contrast with Java primitives (int, double, ...) -- storing them in a reference Array<T> requires boxing them into objects like Integer and Double. The purpose of specialized arrays like IntArray in Kotlin is to store non-boxed primitives, getting rid of boxing and unboxing overhead (the same as Java int[] instead of Integer[]).

You can use Array<String> (and Array<String?> for nullables), which is equivalent to String[] in Java:

val stringsOrNulls = arrayOfNulls<String>(10) // returns Array<String?>
val someStrings = Array<String>(5) { "it = $it" }
val otherStrings = arrayOf("a", "b", "c")

See also: Arrays in the language reference

hotkey
  • 140,743
  • 39
  • 371
  • 326
54

Kotlin's array of Strings

To create an empty array of Strings in Kotlin, you should use one of the following six approaches:


// 01
var empty = arrayOf<String>()

// 02
var empty = arrayOf("","","")

// 03
var empty = Array<String?>(3) { null }

// 04
var empty = arrayOfNulls<String>(3)

// 05
var empty = Array<String>(3) { "it = $it" }

// 06
var empty = Array<String>(0, { _ -> "" })

However, if you need an array of characters, use the following method:

var charArr = charArrayOf('a','b','c')
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
42

use arrayOf, arrayOfNulls, emptyArray

var colors_1: Array<String> = arrayOf("green", "red", "blue")
var colors_2: Array<String?> = arrayOfNulls(3)
var colors_3: Array<String> = emptyArray()
Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40
12

Those types are there so that you can create arrays of the primitives, and not the boxed types. Since String isn't a primitive in Java, you can just use Array<String> in Kotlin as the equivalent of a Java String[].

zsmb13
  • 85,752
  • 11
  • 221
  • 226
6

For Strings and other types, you just use Array<*>. The reason IntArray and others exist is to prevent autoboxing.

So int[] relates to IntArray where Integer[] relates to Array<Int>.

nhaarman
  • 98,571
  • 55
  • 246
  • 278
5

Some of the common ways to create a String array are

  1. var arr = Array(5){""}

This will create an array of 5 strings with initial values to be empty string.

  1. var arr = arrayOfNulls<String?>(5)

This will create an array of size 5 with initial values to be null. You can use String data to modify the array.

  1. var arr = arrayOf("zero", "one", "two", "three")

When you know the contents of array already then you can initialise the array directly.

  1. There is an easy way for creating an multi dimensional array of strings as well.

    var matrix = Array(5){Array(6) {""}}

    This is how you can create a matrix with 5 rows and 6 columns with initial values of empty string.

Himanshu Garg
  • 521
  • 5
  • 3
1

you can use too:

val frases = arrayOf("texto01","texto02 ","anotherText","and ")

for example.

0

This example works perfectly in Android

In kotlin you can use a lambda expression for this. The Kotlin Array Constructor definition is:

Array(size: Int, init: (Int) -> T)

Which evaluates to:

skillsSummaryDetailLinesArray = Array(linesLen) {
        i: Int -> skillsSummaryDetailLines!!.getString(i)
}

Or:

skillsSummaryDetailLinesArray = Array<String>(linesLen) {
        i: Int -> skillsSummaryDetailLines!!.getString(i)
}

In this example the field definition was:

private var skillsSummaryDetailLinesArray: Array<String>? = null

Hope this helps

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2288580
  • 2,210
  • 23
  • 16