Tuesday, June 20, 2017

Kotlin: Array and Loop

Welcome to kotlin Series

kotlin
Kotlin Series

In this post, we are going to learn about Array. The array is really helpful for saving the collection of data. Let's start- we declare a variable and name arrays and assign with Array.
val arrays = Array(5) { 0 }
Now currently our arrays variable has only one index and index position is 0 now add some other index and also print an index for checking.
val arrays = Array(5) { 0 }

arrays[1] = 1
arrays[2] = 2
println(arrays[2])
Note: here Array(5) it indicates that it's size is 5. See an another example-
val arrayStr = Array(5) { "Shudipto" }
arrays[1] = 2
println(arrayStr[0])
Code analysis-
if we don't define this array with a data type then we can insert any type of data type in it. Now add an another variable and initialize it by calling listOf()
val arraysNew = listOf(1, "shudipto", 2.5, 0.234, "Trafder", false)
Code analysis- you might notice that in this array we insert
1. integer
2. string
3. double
4. float
5. boolean
many data types. It's work fine. we don't put the same type of data in the array. That's are array declaring.

Now we use some practices with the loop that's print all the value in an array. first, we using while loop we create a variable that value is zero first and after working with loop this value will increase.
While Loop
var size = 0

//using while loop
while (size < arraysNew.size) {
    println("Position $size value ${arraysNew[size]}")
    size++

}
For Loop:
// using for loop
for (i in arraysNew) {
    println("value: $i")
}
Note: here i is predefined in kotlin it looks like Val i If we want to print value with position then we need two variable in for loop. In the previous we have one variable now we need two variables. How can we do that? see the code-
for ((position, i) in arraysNew.withIndex()) {
     println("position: $position value: $i")
}
Now we do some advance practice: 1. we create a variable and fill this variable this user input after that we show all the data that in the array we for loop(essay to use). we also know how to use for loop.(we just learn it)
for ((position) in arrayFill.withIndex()) {
    print("Insert a value:")
    val input = readLine()!!.toInt()
    arrayFill[position] = input
}
Code Analysis: we need the only position so we declare only one variable. (In the previous code we work with two variables and because we also learn the value along with the position). we also need an array with the index. if don't understand below the line, ignore this list for now. This is null safety. I will talk about null safety in another post.
val input = readLine()!!.toInt()
Now time to print what we are inserted in the array. Try yourself. You already learn how to print all the data of any array. Code-
for ((position, i) in arrayFill.withIndex()) {
    println("position: $position value: $i")
}
Today Full Code-
/**
 * Created by Shudipto Trafder on 6/20/2017.
 */

fun main(args: Array<String>) {

    val arrays = Array(5) { 0 }

    arrays[1] = 1
    arrays[2] = 2
    println(arrays[2])

    val arrayStr = Array(5) { "Shudipto" }
    arrays[1] = 2
    println(arrayStr[0])

    val arraysNew = listOf(1, "shudipto", 2.5, 0.234, "Trafder", false)

    var size = 0

    //using while loop
    while (size < arraysNew.size) {
        println("Position $size value ${arraysNew[size]}")
        size++

    }

    // using for loop
    for (i in arraysNew) {
        println("value: $i")
    }

    for ((position, i) in arraysNew.withIndex()) {
        println("position: $position value: $i")
    }

    println("Please help us to fill some data")
    val arrayFill = Array(5) { 0 }
    for ((position) in arrayFill.withIndex()) {
        print("Insert a value:")
        val input = readLine()!!.toInt()
        arrayFill[position] = input
    }

    println("Your inserted data")
    for ((position, i) in arrayFill.withIndex()) {
        println("position: $position value: $i")
    }
}

Thank you very much for reading this post.
Happy coding

About Author:

I am Shudipto Trafder,
Since 2015, I write android code. I love to implement new ideas. I use java and also kotlin. Now I am learning Flutter. I love to learn new things and also love to share. And that is the main reason to run this blog.


Let's Get Connected: Facebook Twitter Linkedin Github

No comments :