Monday, February 19, 2018

How to initialize array in kotlin

Hello friends today we will see how to initialize array in Kotlin.

1. Lets have a String array.


//In Java
String[] mLabel={"One","Two","Three","Four","Five"}
// In Kotlin 
var mLabel = arrayOf<String>("One", "Two", "Three", "Four", "Five")

2. Lets have int array


// In Java
int[] numbers = {1,2,3,4,5}
// In Kotlin


val numbers : IntArray = intArrayOf(1,2,3,4,5) 
3. Create array of size and initialize with value lambda

 val nu : IntArray =kotlin.IntArray(5) 
  val num : IntArray = kotlin.IntArray(5,{i->1})
     Create int array of size 5 and initializes with 1

No comments:

Post a Comment