Month: May 2021

Kotlin example for Beginners – Kotlin Program to Convert Map (HashMap) to List

Kotlin Program to Convert Map (HashMap) to List In this program, you’ll learn different techniques to convert a map to a list in Kotlin. Example: Convert Map to List import java.util.ArrayList import java.util.HashMap fun main(args: Array<String>) { val map = HashMap<Int, String>() map.put(1, “a”) map.put(2, “b”) map.put(3, “c”) map.put(4, “d”) map.put(5, “e”) val keyList = …

Kotlin example for Beginners – Kotlin Program to Get Current Working Directory

Kotlin Program to Get Current Working Directory In this program, you’ll learn to get the current working directory in Kotlin. Example 1: Get current working directory fun main(args: Array<String>) { val path = System.getProperty(“user.dir”) println(“Working Directory = $path”) } When you run the program, the output will be: Working Directory = C:UsersAdminDesktopcurrDir In the above …

Kotlin example for Beginners – Kotlin Program to Add Two Integers

Kotlin Program to Convert List (ArrayList) to Array and Vice-Versa In this program, you’ll learn to convert a list to an array using toArray() and array to list using asList() in Kotlin. Example 1: Convert array list to array fun main(args: Array<String>) {   // an arraylist of vowels val vowels_list: List<String> = listOf(“a”, “e”, …

Kotlin example for Beginners – Kotlin Program to Concatenate Two Arrays

Kotlin Program to Concatenate Two Arrays In this program, you’ll learn to concatenate two arrays in Kotlin using arraycopy and without it. Example 1: Concatenate Two Arrays using arraycopy import java.util.Arrays fun main(args: Array<String>) { val array1 = intArrayOf(1, 2, 3) val array2 = intArrayOf(4, 5, 6) val aLen = array1.size val bLen = array2.size …

Kotlin example for Beginners – Kotlin Program to Print an Array

Kotlin Program to Convert String to Date In this program, you’ll learn to convert string to date in Kotlin using formatter. Example 1: Convert String to Date using predefined formatters import java.time.LocalDate import java.time.format.DateTimeFormatter fun main(args: Array<String>) { // Format y-M-d or yyyy-MM-d val string = “2017-07-25” val date = LocalDate.parse(string, DateTimeFormatter.ISO_DATE) println(date) } When …

Kotlin example for Beginners – Kotlin Code To Create Pyramid and Pattern

Kotlin Code To Create Pyramid and Pattern In this program, you’ll learn to create pyramid, half pyramid, inverted pyramid, Pascal’s triangle and Floyd’s triangle sing control statements in Kotlin.   Programs to print triangles using *, numbers and characters Example 1: Program to print half pyramid using * * * * * * * * …

Kotlin example for Beginners – Kotlin Program to Add Two Complex Numbers

Kotlin Program to Add Two Complex Numbers by Passing Class to a Function In this program, you’ll learn to add two complex numbers in Kotlin by creating a class named Complex and passing it into a function add(). Example: Add Two Complex Numbers class Complex(internal var real: Double, internal var imag: Double) fun main(args: Array<String>) …

Kotlin example for Beginners – Kotlin Program to Count the Number of Vowels and Consonants in a Sentence

Kotlin Program to Count the Number of Vowels and Consonants in a Sentence In this program, you’ll learn to count the number of vowels, consonants, digits and spaces in a given sentence in Kotlin. Example 1: Program to count vowels, consonants, digits and spaces fun main(args: Array<String>) { var line = “This website is aw3som3.” …