dir.by  
Programming, development, testing
Kotlin
Create collections list, set, map and an array array in Kotlin
  Looked at 1419 times    
 Create collections list, set, map and an array array in Kotlin 
last updated: 3 February 2025
  Kotlin  
fun main() {

     // array
     val my1:Array<Int> = arrayOf<Int>(1, 2, 3) // bad practise
     val my2:IntArray = intArrayOf (10, 20, 30, 40, 50) // good practise

     // list
     val books1:ArrayList<String> = arrayListOf("1", "2");
     val books2:List<String> = listOf("1", "2");
     val books3:MutableList<String> = mutableListOf("1", "2");

     // set
     val numbers:Set<String> = setOf("1", "2", "3", "4")

     // map
     val booksToPrice:Map<String,Int> = mapOf("Green planet" to 10, "Angels and demons" to 25, "Sky flight" to 7)
}
Primitive types
If you use primitives in an object-type array, this has a performance impact because your primitives are boxed into objects.
To avoid boxing overhead, use primitive-type arrays instead.
Primitive type array in Kotlin
Equivalent in Java
BooleanArray
boolean[]
ByteArray
byte[]
CharArray
char[]
DoubleArray
double[]
FloatArray
float[]
IntArray
int[]
LongArray
long[]
ShortArray
short[]
 
← Previous topic
Generic, template class in Kotlin | Example: class MyBook<T> { ... }
 
Next topic →
null value, use the symbol ? and ?. and !! and ?: and !!. and ?. in Kotlin
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

Экскурсии по Москве Экскурсии по Москве: пешеходные, автобусные и речные прогулки на любой вкус
  Объявления  
  Объявления  
 
What is Kotlin?
Why is IntelliJ IDEA the most popular development environment for Kotlin?
Download and install IntelliJ IDEA to explore Kotlin
Create a new project in IntelliJ IDEA for study Kotlin
Int, Float, Boolean, Char ...
Integer numbers in Kotlin are: Byte, UByte, Short, UShort, Int, UInt, Long, ULong
Decimal numbers in Kotlin are: Float, Double
A flag with values of true or false in Kotlin is: Boolean
The symbol in Kotlin is: Char
Convert number to text in Kotlin | Int → String
String
The string, the text in Kotlin is: String
Interpolate strings in Kotlin. Example: val address:String = "${street}, ${country}"
What is the difference between String and StringBuilder ?
Enum
What is an enumeration (enum) in Kotlin ?
How do I find enum by value in Kotlin ?
Class
What is class in Kotlin? Example: class MyBook { ... }
lateinit is a late initialization for the class field | Kotlin
class that inherits from interface in Kotlin | Example: class MyBook : IBook { ...}
Unnamed class that inherits from interface in Kotlin | Example: val book1 = object : IBook { ...}
Generic, template class in Kotlin | Example: class MyBook<T> { ... }
Collections and Arrays
Create collections list, set, map and an array array in Kotlin
null
null value, use the symbol ? and ?. and !! and ?: and !!. and ?. in Kotlin
Lambda function
Lambda function in Kotlin. Example 1: var myFunc1 : (a:Int, b:Int) -> Int = { p1, p2 -> p1 + p2 };

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика