Articles

How to Create an empty arraylist in kotlin and Some Functions of Kotlin ArrayList

by Prayag Ch InterviewQueries Read Best Intervie
How to Create an empty arraylist in kotlin and Some Functions of Kotlin ArrayList

The main difference is that Collections. emptyList() returns an immutable list, i.e., a list to which you cannot add elements:-
 
There are many way to create empty array list:- 
Example:-
 
val list = listOf<String>()
println("list.isEmpty() is ${list.isEmpty()}") // true

// another way to create an empty list,
// type parameter is inferred from the expected type
val other: List<Int> = emptyList()

// Empty lists are equal

println("list == other is ${list == other}") // true
println(list) // []
// list[0] //  will fail

So we found the both method are same but the method of writing changes, but if the output is seen then both are equal and their value remains the same.

Kotlin ArrayList Class

Kotlin arraylist class uesd to creat dynamic array. It's means with the help of ArrayList class can be increase and decrease according to requirement.
 
The elements of ArrayList class are accessed randomly as it works on index basis.

Kotlin ArrayList of constructor

ArrayList<E>()
With the help of this constructor we create an empty ArrayList

ArrayList(capacity: Int)
In this constructor we create an ArrayList of specified capacity.

ArrayList(elements: Collection<E>)
It is used to create an ArrayList filled from the elements of collection.


Some Functions of Kotlin ArrayList
Kotlin arraylist has many function but we discuss here some best function and there uses

open fun add(element: E): Boolean
In this finction used to add the specific element into the collection.

open fun addAll(elements: Collection<E>): Boolean
It is used to add all the elements in the specified collection to current collection.

open fun addAll(elements: Collection<E>): Boolean
It is used to add all the elements in the specified collection to current collection.


open fun set(index: Int, element: E): E
This function used to replaces the element from the specified position from current list with the specified element.

open fun removeRange(startIndex: Int, endIndex: Int)
with the help of this features remove the range of elements starting from startIndex to endIndex in which endIndex is not includes.

open fun remove(element: E): Boolean
It is used to remove a single instance of the specific element from current collection, if it is available.

open fun indexOf(element: E): Int
It is used to return the index of first occurrence of specified element in the list or return -1 if the specified element in not present in the list.

open fun toArray(): Array<Any?>
It is used to return new array of type Array<Any?> with the elements of this collection.


Kotlin Array Declaration
 
We found the different ways we can initialize an Array. We use the array function because To set and initialize the elements of an Array, 
we can use arrayOf function.

val citys = arrayOf("allahabad","kolkata","mumbai","lucknow","delhi")

Apparently the Kotlin compiler defines the type of the array. To specify the type, we can set the function as follows:

val countries = arrayOf<String>("allahabad","kolkata","mumbai","lucknow","delhi") 
//an array of Strings

So what we found that A generic array of type Any can be defined as :

val myArray = arrayOf("Hi",1,1L,1.2,false)

val array1 = arrayOf(1,2,3,4)
val array2 = arrayOf(1L,2L,3L,4L)
val array3 = arrayOf<Long>(1,2,3,4)
//this is the same as array2. An array of longs


Thank You, For Reading Some Array Topic 

Read here For short Kotlin Interview question & answer 










 

Sponsor Ads


About Prayag Ch Innovator   InterviewQueries Read Best Intervie

29 connections, 0 recommendations, 82 honor points.
Joined APSense since, June 17th, 2019, From noida, India.

Created on Dec 31st 2019 06:13. Viewed 732 times.

Comments

Punit Tyagi Freshman  Web & android developer
This is good one Brother, Looked nice to read,
Feb 12th 2020 00:28   
Please sign in before you comment.