How to create ArrayList instances in Kotlin while explicitly assigning their types

As a little note to self, here are some examples of how to create ArrayList instances in Kotlin while explicitly assigning their types:

import java.util.AbstractCollection
import java.util.AbstractList

fun main(args: Array<String>) {
    val a: ArrayList<Int>          = arrayListOf(1,2,3)
    val b: AbstractList<Int>       = arrayListOf(1,2,3)
    val c: MutableList<Int>        = arrayListOf(1,2,3)
    val d: AbstractCollection<Int> = arrayListOf(1,2,3)
    val e: MutableCollection<Int>  = arrayListOf(1,2,3)
    val f: Collection<Int>         = arrayListOf(1,2,3)
    val g: MutableIterable<Int>    = arrayListOf(1,2,3)
    val h: Iterable<Int>           = arrayListOf(1,2,3)
    val i: List<Int>               = arrayListOf(1,2,3)
}

 

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.