Property
ArrayList in kotlin has one property i.e. size. It returns the number of element in the ArrayList.
Example :
Java
fun main(args : Array<String>) { var arrList = arrayListOf<String>(); println(arrList.size); arrList.add( "w3wiki" ); println(arrList.size); } |
Output :
0 1
Kotlin arrayListOf()
The arrayList() is function of Kotlin ArrayList class, which is used to create a new ArrayList. ArrayList is mutable which means that we can modify the content of ArrayList.
Syntax:
fun arrayListOf()
It is used to create an empty new ArrayList.
fun arrayListOf(vararg elements: T)
It is used to create a new ArrayList with provided elements.
Example 1: Kotlin program to make new empty ArrayList.
Java
fun main(args : Array<String>) { var arrList = arrayListOf<String>() println(arrList.isEmpty()) println( "ArrayList : ${arrList}" ) } |
Output :
true ArrayList : []
Example 2: Kotlin program to make new ArrayList with String elements
Java
fun main(args : Array<String>) { var arrList = arrayListOf<String>( "Java" , "Python" , "JavaScript" ) println(arrList.isEmpty()) println( "ArrayList : ${arrList}" ) } |
Output :
false ArrayList : [Java, Python, JavaScript]
Example 3: Kotlin program to make new ArrayList with elements of any data type
Java
fun main(args : Array<String>) { var arrList = arrayListOf<Any>( 1 , 2 , 3 , "w3wiki" , 100.0 ) println(arrList.isEmpty()) println( "ArrayList : ${arrList}" ) } |
Output :
false ArrayList : [1, 2, 3, w3wiki, 100.0]