Articles on Trending Technologies

Technical articles with clear explanations and examples

How to remove an item from an ArrayList in Kotlin?

Soumak De
Soumak De
Updated on 01-Mar-2022 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

Equivalent of getClass() for KClass in Kotlin

Soumak De
Soumak De
Updated on 01-Mar-2022 355 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

How to extend and implement at the same time in Kotlin?

Soumak De
Soumak De
Updated on 01-Mar-2022 770 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
Soumak De
Updated on 01-Mar-2022 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

What's the difference between "!!" and "?" in Kotlin?

Soumak De
Soumak De
Updated on 01-Mar-2022 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

Override getter for Kotlin data class

Soumak De
Soumak De
Updated on 01-Mar-2022 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

Return the result of the negative power to which the input value is raised with scimath in Python

AmitDiwan
AmitDiwan
Updated on 01-Mar-2022 278 Views

To return the result of the power to which the input value is raised with scimath, use the scimath.power() method in Python. Returns x to the power p, i.e. the result of x**p. If x and p are scalars, so is out, otherwise an array is returned. If x contains negative values, the output is converted to the complex domain. The parameter x is the input valueThe parameter p is the power(s) to which x is raised. If x contains multiple values, p has to either be a scalar, or contain the same number of values as x. In the ...

Read More

Replace NaN with zero and fill negative infinity for complex input values in Python

AmitDiwan
AmitDiwan
Updated on 01-Mar-2022 279 Views

To replace NaN with zero and infinity with large finite numbers, use the numpy.nan_to_num() method in Python. The method returns, x, with the non-finite values replaced. If copy is False, this may be x itself. The 1st parameter is the input data. The 2nd parameter is copy, whether to create a copy of x (True) or to replace values in-place. The in-place operation only occurs if casting to an array does not require a copy. Default is True.The 3rd parameter is nan, the value to be used to fill NaN values. If no value is passed then NaN values will ...

Read More

Evaluate a Hermite series at multidimensional array of points x in Python

AmitDiwan
AmitDiwan
Updated on 01-Mar-2022 213 Views

To evaluate a Hermite series at points x, use the hermite.hermval() method in Python Numpy. The 1st parameter, x, if x is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, x or its elements must support addition and multiplication with themselves and with the elements of c.The 2nd parameter, C, an array of coefficients ordered so that the coefficients for terms of degree n are contained in c[n]. If c is multidimensional the remaining indices enumerate multiple polynomials. In the two dimensional case the coefficients ...

Read More

Generate a Vandermonde matrix of given degree with float array of points in Python

AmitDiwan
AmitDiwan
Updated on 28-Feb-2022 221 Views

To generate a Vandermonde matrix of given degree, use the polynomial.polyvander() in Python Numpy. The method returns rhe Vandermonde matrix. The shape of the returned matrix is x.shape + (deg + 1, ), where the last index is the power of x. The dtype will be the same as the converted x.The parameter, a is Array of points. The dtype is converted to float64 or complex128 depending on whether any of the elements are complex. If x is scalar it is converted to a 1-D array. The parameter, deg is the degree of the resulting matrix.StepsAt first, import the required ...

Read More
Showing 45651–45660 of 61,297 articles
Advertisements