123

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?

1

8 Answers 8

158

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>()
Sign up to request clarification or add additional context in comments.

5 Comments

What about typed arrays like IntArray? Right now I am using arrayOf<Int>().toIntArray(), is there any better way?
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.
@csharpfolk, you may use intArrayOf() to initialize an empty IntArray
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 below for details.
@csharpfolk IntArray(size = 2)
50

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

var arr = emptyArray<String>()

See

4 Comments

arr[0] throw java.lang.ArrayIndexOutOfBoundsException: how we can use it?
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.
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.
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 below for details.
23

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.

1 Comment

arr.isEmpty() returns false though. i am guessing because we are fooling kotlin to treat "" as empty?
11

null array

var arrayString=Array<String?>(5){null}
var nullArray= arrayOfNulls<String>(5)

1 Comment

This is an array of nulls, not empty strings as in question.
4

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.

Comments

3

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

Comments

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.

Comments

1

Simplest way to initialise array and assigning values :

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

1 Comment

myArray.isEmpty() returns false though. i am guessing because we are fooling kotlin to treat "" as empty?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.