121

I'm using Array(0, {i -> ""}) currently, and I would like to know if there's a better implementation such as Array()

plus, if I'm using arrayOfNulls<String>(0) as Array<String>, the compiler will alert me that this cast can never succeed. But it's the default implementation inside Array(0, {i -> ""}). Do I miss something?

Mir-Ismaili
  • 13,974
  • 8
  • 82
  • 100
huangcd
  • 2,369
  • 3
  • 18
  • 26
  • http://stackoverflow.com/questions/33583235/how-can-i-tell-kotlin-that-an-array-or-collection-cannot-contain-nulls - one of the comments suggests that the warning is a bug – Duncan McGregor Nov 22 '15 at 00:20

8 Answers8

154

As of late (June 2015) there is the Kotlin standard library function

public fun <T> arrayOf(vararg t: T): Array<T>

So to create an empty array of Strings you can write

val emptyStringArray = arrayOf<String>()
  • 2
    What about typed arrays like `IntArray`? Right now I am using `arrayOf().toIntArray()`, is there any better way? – csharpfolk May 21 '17 at 15:14
  • 6
    what is the purpose of these methods? because we can't assign the size so we can't assign the value or get. array[0] throw `java.lang.ArrayIndexOutOfBoundsException:` exception. – Asif Mushtaq May 26 '17 at 19:24
  • 2
    @csharpfolk, you may use intArrayOf() to initialize an empty IntArray – Cremons Jul 24 '18 at 05:59
  • 1
    This has an immutable length of `0`! So it's useful just for very special purposes that you don't want storing any ***data*** (just want ***type***). Use `Array(length){""}` to include a desired `length`. See [my answer](https://stackoverflow.com/questions/29743160/how-to-create-a-empty-array-in-kotlin#answer-53901415) below for details. – Mir-Ismaili Dec 23 '18 at 05:45
  • 1
    @csharpfolk IntArray(size = 2) – Paul Feb 01 '20 at 09:36
50

Just for reference, there is also emptyArray. For example,

var arr = emptyArray<String>()

See

pt2121
  • 11,720
  • 8
  • 52
  • 69
  • 1
    arr[0] throw `java.lang.ArrayIndexOutOfBoundsException:` how we can use it? – Asif Mushtaq May 26 '17 at 19:24
  • 13
    It's an empty array, you can't add/read anything to/from it. It may seem useless, but sometimes you have to pass an array somewhere and this allows you to construct an empty array easily. – Kuba Beránek Jul 21 '17 at 12:38
  • 2
    this is better than Martian Odyssey's answer because when used in a data class to initialize an array, arrayOf causes a java.util.concurrent.ExecutionException while emptyArray() doesn't. – iSWORD Aug 09 '17 at 17:00
  • 1
    This has an immutable length of `0`! So it's useful just for very special purposes that you don't want storing any ***data*** (just want ***type***). Use `Array(length){""}` to include a desired `length`. See [my answer](https://stackoverflow.com/questions/29743160/how-to-create-a-empty-array-in-kotlin#answer-53901415) below for details. – Mir-Ismaili Dec 23 '18 at 05:50
21

Empty or null? That's the question!

To create an array of nulls, simply use arrayOfNulls<Type>(length).


But to generate an EMPTY array of size length, use:

val arr = Array(length) { emptyObject }

Note that you must define an emptyObject properly per each data-type (beacause you don't want nulls). E. g. for Strings, emptyObject can be "". So:

val arr = Array(3) { "" }  // is equivalent for: arrayOf("","","")

Here is a live example. Note that the program runs with two sample arguments, by default.

Mir-Ismaili
  • 13,974
  • 8
  • 82
  • 100
11

null array

var arrayString=Array<String?>(5){null}
var nullArray= arrayOfNulls<String>(5)
Ali Hasan
  • 653
  • 9
  • 8
4

As mentioned above, you can use IntArray(size) or FloatArray(size).

CoolMind
  • 26,736
  • 15
  • 188
  • 224
3

I found two ways to create an empty array, the second way without a lambda:

var arr = Array (0, { i -> "" })
var arr2 = array<String>()

Regarding Kotlin's null strings, this is not allowed. You have to use String? type to allow strings to be null.

tbruyelle
  • 12,895
  • 9
  • 60
  • 74
Ciprian Khlud
  • 434
  • 3
  • 6
2

Use:

@JvmField val EMPTY_STRING_ARRAY = arrayOfNulls<String>(0)

It returns an 0 size array of Strings, initialized with null values.

1. Wrong:

@JvmField val EMPTY_STRING_ARRAY = emptyArray<String>()

It returns arrayOfNulls<String>(0)

2. Wrong:

 @JvmField val EMPTY_STRING_ARRAY = arrayOf<String>()

It returns an array containing the Strings.

Alexander Savin
  • 1,952
  • 1
  • 15
  • 30
1

Simplest way to initialise array and assigning values :

val myArray: Array<String> = Array(2) { "" }
    myArray[0] = "Java"
    myArray[1] = "Kotlin"