I tried these in Kotlin REPL
var listA = listOf(null ,null) var listB = [null, null]
The first line works fine as expected. On displaying listA I get:
[null, null]
The second line throws the following error:
error: cannot use 'Nothing?' as reified type parameter var listB = [null,null] ^ error: unsupported [Collection literals outside of annotations] var listB = [null,null] ^ error: unsupported [Array<Nothing> in return type is illegal] var listB = [null,null] ^
When I try the same with non null types, i.e.
var listC = [1,2]
I get this error:
error: unsupported [Collection literals outside of annotations] var listC = [1,2] ^
I'm new to Kotlin. Can someone please explain what is going on here?
To define a list of Strings in Kotlin, call listOf() function and pass all the Strings as arguments to it. listOf() function returns a read-only List. Since, we are passing strings for elements parameter, listOf() function returns a List<String> object.
To define a list of integers in Kotlin, call listOf() function and pass all the integers as arguments to it. listOf() function returns a read-only List. Since, we are passing integers for elements parameter, listOf() function returns a List<Int> object.
Kotlin ArrayList class can be used in order to create an empty arrayList. It would be a dynamic array which means it will automatically expand as we add data into it. An ArrayList is an ordered sequence of elements, however, unlike simple arrays, an ArrayList can contain data of multiple data types.
In Kotlin, the default implementation of List is ArrayList which you can think of as a resizable array.
From the Kotlin documentation on Collections:
Kotlin does not have dedicated syntax constructs for creating lists or sets. Use methods from the standard library, such as listOf(), mutableListOf(), setOf(), mutableSetOf().
There are no list literals currently for code outside of annotations.
As @Carcigenicate pointed out, there is not syntax for [null, null]
.
However, Kotlin does have some handy methods for handling lists and arrays.
listOf()
Creates a new read-only List.
listOfNotNull()
Basically the same as listOf(), but without null values. Even empty strings are skipped.
arrayListOf()
Creates an ArrayList. This time you can modify (add/remove) elements.
mutableListOf()
Behaves like arrayListOf(). Actually, mutableListOf() internally uses ArrayLists. Read more
This creates an array, which is very different to most languages you know. Instead of syntax structures like {1, 2, 3} or [1, 2, 3] you have functions in Kotlin. You also get functions for typed array:
- booleanArrayOf()
- doubleArrayOf()
- charArrayOf()
- ...
One exception are annotations, which explains your compiler error [Collection literals outside of annotations]
:
@MyAnnotation(arguments = [1, 2, 3])
However, this could change in the future as discussed here.
Array<Int>
is an Integer[]
under the hood, while IntArray
is a primitive int[]
when targeting the JVM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With