Wednesday, August 16, 2017

Kotlin ArrayList

August 16, 2017

Welcome to kotlin Series

Kotlin

In this post, we are going to learn about ArrayList. In android the use of ArrayList is huge.
So let's move on today task list.
1. we create an ArrayList
2. insert some value
3. update some value
4. search in ArrayList
5. delete some value

Let's start:
Note: we discuss array and loop in this post Kotlin: Array and Loop. you can take a look at this post of array section it will help you to understand today's topics easily.
Create an instance of ArrayList class and make this variable as immutable by using val
val list = ArrayList<String>()
Now insert some value by calling add method
list.add("Shudipto")
list.add("Trafder")
list.add("Is")
list.add("a")
list.add("good")
list.add("student")
Now print the first element of this array list
println("First Name:${list[0]}")
Result:
First Name:Shudipto
Now print all the element by using a for loop. see the code-
println("All element")
println("\nOnly value")
for (a in list){
     println("value:$a")
}
Output:
All element

Only value
value:Shudipto
value:Trafder
value:Is
value:a
value:good
value:student
Now print all the element with the index number. see the code-
println("\nIndex with value")
for ((position,a) in list.withIndex()){
    println("Index:$position value:$a")
}
Output:
Index with value
Index:0 value:Shudipto
Index:1 value:Trafder
Index:2 value:Is
Index:3 value:a
Index:4 value:good
Index:5 value:student
Now update an element-
we update the value of index number 5.
The new value will be developer.
println("\n\nUpdate an element")
list[5] = "developer"
Now print all the value with index
println("\nIndex with value")
for ((position,a) in list.withIndex()){
     println("Index:$position value:$a")
}
Output:
Index with value
Index:0 value:Shudipto
Index:1 value:Trafder
Index:2 value:Is
Index:3 value:a
Index:4 value:good
Index:5 value:developer
Search in ArrayList: we make a search in our ArrayList.
If we found the data and we print name found and if not we print name not found.
see the code below
println("\n\nSearch in array list")
if (list.contains("Shudipto")){
    println("Name found")
} else println("Not found")
Output:
Search in array list
Name found
Now delete some data: we can remove by two ways.
1. by value
2. by index number
I am showing you those two ways by the value, we remove "a" by index number, we remove index number 5
see the code below
println("Delete some data")
//with value
list.remove("a")
//with position
list.removeAt(4)
Note: in code, we use index number 4 because we just remove one element so the last index number is now 4.
Now print all the value with index number
println("\nIndex with value")
for ((position,a) in list.withIndex()){
     println("Index:$position value:$a")
}
Output:
Index with value
Index:0 value:Shudipto
Index:1 value:Trafder
Index:2 value:Is
Index:3 value:good
Today's full code:
fun main(args: Array<String>) {

    val list = ArrayList<String>()

    list.add("Shudipto")
    list.add("Trafder")
    list.add("Is")
    list.add("a")
    list.add("good")
    list.add("student")

    println("First Name:${list[0]}") //list.get(0)

    println("All element")
    println("\nOnly value")
    for (a in list){
        println("value:$a")
    }

    println("\nIndex with value")
    for ((position,a) in list.withIndex()){
        println("Index:$position value:$a")
    }

    println("\n\nUpdate an element")
    //list.set(5,"developer")
    list[5] = "developer"

    println("All element")
    println("\nOnly value")
    for (a in list){
        println("value:$a")
    }

    println("\nIndex with value")
    for ((position,a) in list.withIndex()){
        println("Index:$position value:$a")
    }

    println("\n\nSearch in array list")
    if (list.contains("Shudipto")){
        println("Name found")
    } else println("Not found")

    println("Delete some data")
    //with value
    list.remove("a")
    //with position
    list.removeAt(4)

    println("\nIndex with value")
    for ((position,a) in list.withIndex()){
        println("Index:$position value:$a")
    }
}
Full output:
First Name:Shudipto
All element

Only value
value:Shudipto
value:Trafder
value:Is
value:a
value:good
value:student

Index with value
Index:0 value:Shudipto
Index:1 value:Trafder
Index:2 value:Is
Index:3 value:a
Index:4 value:good
Index:5 value:student


Update an element
All element

Only value
value:Shudipto
value:Trafder
value:Is
value:a
value:good
value:developer

Index with value
Index:0 value:Shudipto
Index:1 value:Trafder
Index:2 value:Is
Index:3 value:a
Index:4 value:good
Index:5 value:developer


Search in array list
Name found
Delete some data

Index with value
Index:0 value:Shudipto
Index:1 value:Trafder
Index:2 value:Is
Index:3 value:good

Process finished with exit code 0
Thank you for reading this post.
Happy coding

Read More