Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create list in Kotlin?

Tags:

kotlin

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?

like image 200
Shreyas Avatar asked May 13 '18 17:05

Shreyas


People also ask

How do I create a string list on Kotlin?

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.

How do I create an integer list in Kotlin?

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.

How do I create an empty list on Kotlin?

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.

Is Kotlin list an ArrayList?

In Kotlin, the default implementation of List is ArrayList which you can think of as a resizable array.


2 Answers

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.

like image 184
Carcigenicate Avatar answered Oct 02 '22 14:10

Carcigenicate


As @Carcigenicate pointed out, there is not syntax for [null, null].
However, Kotlin does have some handy methods for handling lists and arrays.

Lists

  • 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

Arrays

  • arrayOf()

    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.


While working with arrays, it is important to know the return types those functions are creating. As an example: Array<Int> is an Integer[] under the hood, while IntArray is a primitive int[] when targeting the JVM.
like image 30
null Avatar answered Oct 02 '22 14:10

null