Found 1912 Articles for Mobile Development

How to initialize List in Kotlin?

Soumak De
Updated on 16-Mar-2022 14:16:24
List denotes a List collection of generic data type. By , we understand that the List does not have any specific data type. Let's check how we can initialize such a collection in Kotlin.List can be of two types: immutable and mutable. We will see two different implementations of initializing List.Example – Initialize List ~ Immutable ListOnce a list is declared as Immutable, then it becomes main(args: Array) {    var myImmutableList = listOf(1, 2, 3)    // Convert array into mutableList    // Then, add elements into it.    myImmutableList.toMutableList().add(4)    // myImmutableList is not a ... Read More

How to work with Maps in Kotlin?

Soumak De
Updated on 16-Mar-2022 14:07:44
A Map is a collection where data is stored as a key-value pair and the corresponding keys have to be unique. A HashMap is a collection class based on MutableMap interface and it does that by implementing the MutableMap interface of HashTable.Kotlin provides four types of constructors to define and manipulate a HashMap.Example – Map Creation and Iteration in KotlinIn this example, we will be creating a Map called "subject" and we will be iterating through main(args: Array) {    // Declare HashMap    var subject : HashMap       = HashMap ();    // Assigning ... Read More

How to replace duplicate whitespaces in a String in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:47:34
In order to remove extra whitespaces in a string, we will use the replace() function along with toRegex() function from the String class. To replace all the consecutive whitespaces with a single space " ", use the replace() function with the regular expression "\s+" which matches with one or more whitespace characters.Example – Removing extra whitespaces in KotlinTake a look at the following example −fun main(args: Array) {    var myString = "Removing ex tra spa ce from String"    println("Input String: " + myString)    // removing duplicate whitespace    println("Extra whitespaces removed: "    + myString.replace("\s+".toRegex(), " ... Read More

How to use Java's bitwise operators in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:43:09
Kotlin provides a set of bitwise operators that we can use on integers. These operators can be applied only to Int and Long type variables. Here is the list of bitwise operations available in Kotlin −shl(bits) – Signed Shift leftshr(bits) – Signed shift rightushr(bits) – Unsigned shift rightand(bits) – Bitwise AND operatoror(bits) – Bitwise OR operatorxor(bits) – Bitwise XORinv() – Bitwise inversionKotlin does have functions for each of them.Example: Bitwise Operators in KotlinThe following example shows how you can implement the bitwise operators in Kotlin.import java.lang.* fun main(args: Array) {    val value = 5    println("Input value: " + ... Read More

What does "return@" mean in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:38:40
return@ is a statement in Kotlin which helps the developers to return a function to the called function. In simple words, return@ can return any value, anonymous function, simple inline function, or a lambda function.Example – return in Kotlinfun main(args: Array) {    val s = message()    println("Message = $s") } fun message():String{    return "Hello Kotlin! This is a returned message." }OutputIt will produce the following output −Message = Hello Kotlin! This is a returned message.Example – Kotlin Labeled returnNow "return@" helps to control the flow to a specific level inside the code. In Kotlin terminology, it is ... Read More

How to initialize an empty array list in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:32:42
Kotlin ArrayList class can be used in order to create an empty arrayList. It would be a dynamic array which means it will automatically expand as we add data into it. An ArrayList is an ordered sequence of elements, however, unlike simple arrays, an ArrayList can contain data of multiple data types.The function definition of arrayList goes like this −fun arrayListOf(): ArrayListIt returns an empty new ArrayList. If a number is provided as the argument, then it will return an arrayList with the given elements.Example: Initialize an empty array in KotlinThe following example demonstrates how you can create a ... Read More

How to convert an ArrayList to String in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:29:18
In this article, we will see how we can convert an ArrayList in Kotlin to a String. In order to do so, we will use a String function called joinToString() that is provided by the Kotlin library. Its definition goes like this −fun Array.joinToString(    // the String will be separated by comma    separator: CharSequence = ", ",        // prefix to the String    prefix: CharSequence = "",        // postfix to the String    postfix: CharSequence = "",        // This number of elements will be printed;    // the ... Read More

How to check generic type in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:16:35
In this article, we will see how we can get the type of a class that is used in Kotlin. There are no direct ways to do this in Kotlin. In order to check the generic type, we need to create an instance of the generic class and then we can compare the same with our class.ExampleFor this example, we will create a generic class of type and we will pass our variable inside this class in order to check its type.class MyGenericClass(val myclass: Class) {    companion object {       inline operator fun invoke() = MyGenericClass(   ... Read More

Kotlin equivalent of Java's equalsIgnoreCase

Soumak De
Updated on 16-Mar-2022 13:12:21
Java provides a String method called equalsIgnoreCase() which helps developers to compare two strings on the basis of their content. This comparison is case-insensitive, that is, it ignores whether the strings are in uppercase or lowercase and just compares the string values. In this article, we will see how we can implement the same functionality in Kotlin.Example – equalsIgnoreCase in JavaThe following example demonstrates how equalsIgnoreCase() works in Java.public class MyClass {    public static void main(String args[]){       String s1="TutorialsPoint";       String s2="tutorialspoint";       System.out.println("String 1: " ... Read More

How to create an instance of an abstract class in Kotlin?

Soumak De
Updated on 16-Mar-2022 13:03:45
When a class is defined in Kotlin with an abstract keyword, then it is known as an abstract class. In Kotlin, we cannot create an instance of an abstract class. Abstract classes can only be implemented by another class which should be abstract in nature. In order to use an abstract class, we need to create another class and inherit the abstract class.Example – Abstract class in KotlinThe following example demonstrates how you can create an instance of an abstract class in Kotlin.abstract class myInter {    abstract var absVariable : String    abstract fun absMethod() } class myClass : ... Read More
Previous 1 ... 4 5 6 7 8 ... 192 Next