In this tutorial you will learn about Arrays via simple step by step examples.
Example 1: Arrays
Let us look at a simple Arrays example written in Kotlin Programming Language. Follow the following steps:
Step 1: Create Project
- Open your favorite Kotlin IDE.
- In the menu go to
File --> Create New Project
.
Step 2: Add Dependencies
No dependencies are needed for this project.
Step 3: Write Code
Our code will comprise the following Kotlin files:
Arrays.kt
- In your editor or IDE, create a file known as
Arrays.kt
. - Then add the following code:
(a). Arrays.kt
package Collections.Arrays
//Array is low level data structure - only use it when you need to. Consider using ArrayList instead
fun main(args : Array<String>) {
val presidents = arrayOf(Pair("John F", "K"), Pair("Ronald", "Reagan"))
presidents.forEach { println("${it.first} ${it.second}") }
val numbers = arrayOf(1, 2, 3, 4, 5)
numbers.forEach { println(it) }
//specialized primitives arrays for performance
val bools = booleanArrayOf(true, false, true)
val bytes = byteArrayOf(1, 2)
val ints = intArrayOf(1, 2, 3)
val floats = floatArrayOf(1.0f, 2.0f, 3.0f)
val doubles = doubleArrayOf(1.0, 2.0, 3.0)
val longs = longArrayOf(1, 2, 3)
val chars = charArrayOf('1', '2', '3')
val shorts = shortArrayOf(1, 2, 3)
}
Step 4: Run
Copy the code, build and run.
Reference
Here are the reference links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |