Home » Kotlin

Program to Swap two numbers in Kotlin using various ways

(Use of run, also, with and destructuring declarations)

In this article, we are going to learn how to swap two numbers using different methods in Kotlin? Here, we are using simple method, run, also, with and destructuring declarations.
Submitted by Aman Gautam, on January 29, 2018

Previously we have studied several logical methods to swap two numbers but now its turn for Kotlin to show its magic.

In this article, we will learn various methods to swap two numbers in Kotlin. We will also study various methods and block like run, also, with and destructuring declarations in Kotlin.

1) Normal way

fun swap(a:Int,b:Int){
	var a=a
	var b=b

	var temp=a;
	a=b
	b=temp
}

Since the function parameter are val by default so first we have to move the values from val to var. Then only we can perform swap operations.

2) Using run block

run { val temp = a; a = b; b = temp }

Run block is also called scoping block/function as it provides inner scope to the calling functions.

3) Using also block

a = b.also { b=a }

This is similar to a=b and b=a. Executing both simultaneously.

4) Using with function

with(a) {
	a = b
	b = this
}

Here, with contain the reference of a and then using this keyword, it is passed to b.

5) Swapping mutable values

    val c = 1
    val d = 2
    val (a2, b2) = d to c

Here, we have use destructuring declarations and d to c is working as Pair (d,c) which automatically assigns a2=d and b2 to c.

Kotlin program to swap two numbers using different ways

fun main(args: Array<String>) {
    var a=5;
    var b=10
    // way 1
    println("Using also")
    println("values before swapping ")
    println("a = "+a+" b= "+b)

    a=b.also { b=a }

    println("values after swapping ")
    println("a = "+a+" b= "+b)

    //way 2
    println("\nUsing run block")
    println("values before swapping ")
    println("a = "+a+" b= "+b)

    run { val temp = a; a = b; b = temp }

    println("values after swapping ")
    println("a = "+a+" b= "+b)

    //way 3
    println("\nUsing with method")
    println("values before swapping ")
    println("a = "+a+" b= "+b)
    with(a) {
        a = b
        b = this
    }
    println("values after swapping ")
    println("a = "+a+" b= "+b)

    //way 4
    println("\nSwapping mutable values using destructuring declaration")
    val c = 1
    val d = 2
    println("values before swapping ")
    println("a = "+c+" b= "+d)


    val (a2, b2) = d to c

    println("values after swapping ")
    println("a = "+a2+" b= "+b2)
}

Output

Using also
values before swapping 
a = 5 b= 10
values after swapping 
a = 10 b= 5

Using run block
values before swapping 
a = 10 b= 5
values after swapping 
a = 5 b= 10

Using with method
values before swapping 
a = 5 b= 10
values after swapping 
a = 10 b= 5

Swapping mutable values using destructuring declaration
values before swapping 
a = 1 b= 2
values after swapping 
a = 2 b= 1


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.