Remove Item from ArrayList in Kotlin

Soumak De
Updated on 01-Mar-2022 11:13:33

2K+ Views

In this article we will see how we can remove an item from an ArrayList using Kotlin library function. In order to do that, we will take the help of a library function called drop(). The function definition looks as follows −fun Array.drop(n: Int): List (source)It takes array and a position as an input and it returns a list containing all the elements except the first n elements.Example – drop() in KotlinIn this example, we will remove the first element from a list using drop().fun main(args: Array) {    var arrayone: ArrayList = arrayListOf("mango", "jam", "apple", "lemon", "spice")   ... Read More

Create a List in Kotlin

Soumak De
Updated on 01-Mar-2022 11:09:32

1K+ Views

A list is a collection to hold same type of data in a single variable. Kotlin does not provide any dedicated literal to create a collection. As per the documentation of Kotlin, a List is an ordered collection with access to elements by indices.In Kotlin, we do have two different kinds of collections; one is read-only which is known as immutable collection and the other type of collection is where we can have a write facility as well which is known as mutable collection.In this article, we will see how we can create these two types of lists and manipulate ... Read More

Create an Empty Array in Kotlin

Soumak De
Updated on 01-Mar-2022 11:01:38

492 Views

An array is a collection where we can store multiple items of the same type. We can consider an array of integers or an array of strings. This is very useful, for example, whenever we need to store the name of 1000 students in a single variable.Example – Using arrayOf()In this example, we will see how we can create an empty array in Kotlin. We will be creating an empty array of Strings and manipulate the same in the program.fun main(args: Array) {    // Declareting empty array of type String    val emptyStringArray = arrayOf()    println("Example of empty ... Read More

Equivalent of getClass for KClass in Kotlin

Soumak De
Updated on 01-Mar-2022 10:56:29

321 Views

In this article, we will take an example and demonstrate how we can obtain the class reference in Kotlin. Kotlin does not support fetching the class reference directly, but you can obtain the same reference via extension itself. In the following example, we will see how we can do it via Kotlin library functions.Example – Class reference using KClassIn this example, we will get the reference of the class.import kotlin.reflect.KClass fun main(args : Array) {    // to get the reference of the class    fun T.getClass(): KClass {       return javaClass.kotlin    }    val ... Read More

Extend and Implement at the Same Time in Kotlin

Soumak De
Updated on 01-Mar-2022 10:51:36

735 Views

In this article, we will take an example to demonstrate how we can extend and implement in the same class. In this example, We will be creating an interface and a dummy Parent class.From the Child class, we will extend the Parent class and implement the interface.Example – Extending and implementing in childinterface myInterface {    fun test(): String } open class myParentClass(val name:String) {    fun anotherTest():String {       return name    } } class Child() : myInterface, myParentClass("ParentClass Input"){    //child extending the parent class and implementing myInterface    override fun test(): String { ... Read More

Why Can't Kotlin Result Be Used as a Return Type

Soumak De
Updated on 01-Mar-2022 10:44:58

1K+ Views

Result is a Serializable class in Kotlin. The function definition looks like this −class Result : SerializableThis class has two properties − "isFailure" and "isSuccess".As per the documentation, Result cannot be used as a direct return type of Kotlin function. However, in this article, we will see how we can use this Result in our Kotlin programs.// ERROR: 'kotlin.Result' cannot be used as a return type fun findUserByName(name: String): Result fun foo(): Result      // ERROR fun foo(): Result?          // ERROR var foo: Result             // ERRORExample – Kotlin.Resultsealed class ... Read More

Difference Between 'and' and '&&' in Kotlin

Soumak De
Updated on 01-Mar-2022 10:38:44

1K+ Views

In this article, we will take an example and demonstrate the difference between (!!) and (?) in Kotlin.Example – "!!" and "?" operator in KotlinKotlin provides a wonderful operator to check NULL pointer exceptions. It throws a NULL pointer exception instead of breaking the programming logic whenever the variable is NULL.In the following example, the value of "test" is NULL. Hence, Kotlin will throw a NULL pointer exception instead of breaking down the logic. The example shows the different uses of "!!" and "?" operators.fun main(args: Array) {    val nullValue: String ?=null    // it will print null ... Read More

Throw Custom Exception in Kotlin

Soumak De
Updated on 01-Mar-2022 10:33:05

406 Views

Exception is an important aspect of any programming language. It prevents our code from generating incorrect output at runtime. There are two types of exceptions −Checked exceptionsUnchecked exceptionsChecked ExceptionsChecked exceptions are those which are checked at the compile time. As per example, FileNotFoundException() or IOException. In the following example, we will see how we can generate a checked Exception.Exampleimport java.io.File import java.io.InputStream fun main(args: Array) {    try {       val inputStream: InputStream = File("Hello.txt").inputStream()    } catch(e:Exception) {       e.printStackTrace();    } }OutputOnce you execute this code, it will generate the following output in ... Read More

Override Getter for Kotlin Data Class

Soumak De
Updated on 01-Mar-2022 09:59:38

3K+ Views

Data class is a collection in Kotlin to store data. Logically this is same as creating a Java POJO class. In Java, we do create extra member functions to set and get the data in the member variables. In Kotlin, we don’t need to create separate methods to access the member variables of the class. Kotlin provides it by default. We can access the variables by just using the member variable names in it.It is not recommended to override any of the data class members like we used to do in Java.It is always recommended to use normal class if ... Read More

Cumulative Product of Array Elements Treating NaNs as One in Python

AmitDiwan
Updated on 01-Mar-2022 08:11:37

171 Views

To return the cumulative product of array elements over a given axis treating NaNs as one, use the nancumprod() method. The cumulative product does not change when NaNs are encountered and leading NaNs are replaced by ones. Ones are returned for slices that are all-NaN or empty. The method returns a new array holding the result is returned unless out is specified, in which case it is returned. Cumulative works like, 5, 5*10, 5*10*15, 5*10*15*20The 1st parameter is the input array. The 2nd parameter is the Axis along which the cumulative product is computed. By default the input is flattened. ... Read More

Advertisements