Server-Side Kotlin-Array Methods

Server-Side Kotlin-Array Methods

·

1 min read

Component1():-

This method returns 1st element from the given array.

>>>val languages = arrayOf<String>("java","python", "javascript","php")
>>>println(languages.componenet1())
java

Component 2/3/4:-

This method returns 2,3 and 4th elements from the given array.

first():-

Returns the first element from the given array.

>>>println(languages.first())
java

lastIndexOf():-

Returns the last index number of elements from the given array.

>>>println(languages.lastIndexOf("php"))
3

none():-

Returns true if the array has no elements.

>>>println(languages.none())
false
//empty array
>>>val lang = arrayOf<String>()
>>>println(languages.none())
true

random():-

Returns a random element from the given array.

>>>println(languages.random())
php

take():-

Returns a list containing the first n elements.

>>>println(languages.take(2))
[java,python]

contains():-

Returns true if the element is found in the array.

>>>println(languages.contains("javascript"))
true

count():-

Returns the total number of elements from the given array.

>>>println(languages.count())
4

drop():-

Returns a list containing all elements except the first n elements.

>>>println(languages.drop(2))
[javascript, php]

code tested on kotlin REPL Kotlin version 1.7.21 (JRE 11.0.17+8-post-Ubuntu-1ubuntu220.04)