dir.by  
Поиск >>

Компьютер, программы
→
Kotlin
 Arrays in Kotlin: class Array<T>, class IntArray, class FloatArray. Functions to create: arrayOf(...), intArrayOf(...), floatArrayOf(...) 
посмотрели 1206 раз
обновлено: 25 September 2026
class Array<T> is an array (in other words, a collection of elements).
Any element can be gotten by integer index.
You cannot add or delete elements.

  Kotlin     library kotlin-stdlib.jar the Array.kt file
class Array<T> {
     public inline constructor(size: Int, init: (Int) -> T)

     public val size: Int

     public operator fun get(index: Int): T
     public operator fun set(index: Int, value: T): Unit
     public operator fun iterator(): Iterator<T>
}

 
To create Array<T>, use the function:
  Kotlin  
fun arrayOf() : Array<T>


  Kotlin     Example, create an Array
val myArray1:Array<String> = arrayOf("Book1", "Book2")


Very important! If you use primitives (Int, Long, Float, Double, etc.) in the Array class like this:
Array<Int> or Array<Long> or Array<Double> or Array<Float>
This affects performance because primitives reside in memory as objects. That is, the primitive is packed: memory is allocated for the primitive and the primitive is copied to this memory. Then this address is returned. This address is added to Array.
  Kotlin  
     // array
val my1:Array<Int> = arrayOf(10, 20, 30) // bad practis
val my2:IntArray = intArrayOf(10, 20, 30, 40, 50) // good practise

In IntArray, all values are stored in one contiguous chunk of memory, with no references.
Primitive arrays in Kotlin and Java
Primitive array in Kotlin
Primitive array in Java
BooleanArray
ByteArray
CharArray
DoubleArray
FloatArray
IntArray
LongArray
ShortArray
boolean[]
byte[]
char[]
double[]
float[]
int[]
long[]
short[]
What is the difference between IntArray and ArrayList<Int>
Advantages: ArrayList<Int>
1.
Dynamic resizing: Unlike arrays, ArrayList can resize dynamically, which means you can add or remove elements from the list as needed.
2.
Flexible storage: ArrayList can store objects of any type, making it useful for storing collections of any data.
3.
Easy manipulation: ArrayList provides a variety of methods for adding, removing, and manipulating elements in the list, making it easy to work.
Disadvantages: ArrayList<Int>
1.
Overhead: ArrayList has a small amount of overhead compared to arrays, which can affect performance in certain situations.
2.
Memory usage: ArrayList can use more memory than arrays because it stores each element as an object, which requires additional memory overhead.
3.
Iteration performance: When iterating over an ArrayList, accessing elements by index can be slower than using an array, because each element is accessed through a function call rather than direct memory access.
Inference: ArrayList is a powerful and flexible collection type that is useful in many situations, but it may not always be the best choice depending on the specific requirements of your program.
 
← Previous topic
Lists in Kotlin: interface List , interface MutableList , class ArrayList. Functions to create: listOf(...), mutableListOf(...), arrayListOf(...)
 
Next topic →
fun() { ... } is a function in Kotlin. The function is needed to perform an action.
 
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 console project to explore Kotlin | IntelliJ IDEA
Setting the value
• Declare the variable and set the value in Kotlin | val, var, lateinit, constant: const val, global variable: companion object
null
• null value, use the symbol ? and ?. and !! and ?: and !!. and ?. in 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 ?
Collections and Arrays
• Collections in Kotlin
• Lists in Kotlin: interface List , interface MutableList , class ArrayList. Functions to create: listOf(...), mutableListOf(...), arrayListOf(...)
• Arrays in Kotlin: class Array<T>, class IntArray, class FloatArray. Functions to create: arrayOf(...), intArrayOf(...), floatArrayOf(...)
Function
• fun() { ... } is a function in Kotlin. The function is needed to perform an action.
Extension function
• Extension function in Kotlin. Allows you to add a new function to the system (List, ..) or my (Book, ...) class
Lambda function
• Lambda function in Kotlin. Example 1: var myFunc1 : (a:Int, b:Int) -> Int = { p1, p2 -> p1 + p2 };
Class
• What is class in Kotlin? Example: class MyBook { ... } | Primary and secondary constructor | block init
• Access modifiers (private, protected, public) for a class and for methods and fields. | Kotlin
• 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> { ... }
Flow
• flow is the data flow in Kotlin. These are the substitution of LiveData in Android Kotlin and the substitution of Observable in RxJava
Writing a program
• The Kotlin console app works like Android Jetpack Compose

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