Sunday, 3 December 2017

Kotlin: Print array elements

In my previous post, I explained what is an array and how to define it in kotlin. In this post, I am going to show,how to print elements of an array.

Print elements of array using for loop

ArrayUtil.kt
fun main(args: Array<String>) {

 var arr: Array<Int> = arrayOf(1, 2, 3, 4)

 for (ele in arr) {
  println(ele)
 }
}

Output
1
2
3
4

Print array elements using 'Arrays.toString' method

ArrayUtil.kt
import java.util.Arrays

fun main(args: Array<String>) {

 var arr: Array<Int> = arrayOf(1, 2, 3, 4)

 println(Arrays.toString(arr))
}

Output
[1, 2, 3, 4]

Print array elements using array.indices

ArrayUtil.kt

fun main(args: Array<String>) {

 var arr: Array<Int> = arrayOf(2, 3, 5, 7)

 for (index in arr.indices) {
  println(arr[index])
 }
}

Output
2
3
5
7


Print array elements using array.withIndex

ArrayUtil.kt

fun main(args: Array<String>) {

 var arr: Array<Int> = arrayOf(2, 3, 5, 7)

 for ((index, value) in arr.withIndex()) {
  println("index : $index, value : $value")
 }
}

Output
index : 0, value : 2
index : 1, value : 3
index : 2, value : 5
index : 3, value : 7

Print array elements using while loop

ArrayUtil.kt

fun main(args: Array<String>) {

 var arr: Array<Int> = arrayOf(2, 3, 5, 7)

 var sizeOfTheArray = arr.size;
 
 var index : Int = 0;
 
 while(index < sizeOfTheArray){
  println("index : $index, value : ${arr[index]}")
  index++
 }
}

Output

index : 0, value : 2
index : 1, value : 3
index : 2, value : 5
index : 3, value : 7




Previous                                                 Next                                                 Home

Saturday, 2 December 2017

Kotlin: Define array using factory function

Kotlin provides a factory function, which takes size and a function ‘f’ as arguments to define an array. The function ‘f’ is used to provide initial values to the array.

var squares = Array(5, { i -> i * i })
Above statement define an array 'squares' of size 5 and initialize {1, 4, 9, 16, 25}

ArrayFactoryFunction.kt
fun main(args: Array<String>) {

 var squares = Array(5, { i -> i * i })
 var cubes = Array(5, { i -> i * i * i })

 println("Elements in squares array : ")
 for (ele in squares) {
  println(ele)
 }

 println("\nElements in cubes array : ")
 for (ele in cubes) {
  println(ele)
 }

}


Output
Elements in squares array :
0
1
4
9
16

Elements in cubes array :
0
1
8
27
64



Previous                                                 Next                                                 Home

Friday, 1 December 2017

Kotlin: Define array of nulls

Kotlin provides 'arrayOfNulls' library function to define array of null values.

ArrayOfNulls.kt
fun main(args: Array<String>) {

 var a: Array<Int?> = arrayOfNulls(5)

 for (element in a) {
  println(element)
 }

}

Output
null
null
null
null
null


Previous                                                 Next                                                 Home

Thursday, 30 November 2017

Kotlin: Arrays

Array represents a collection of elements of same type. Kotlin provides Array class to represent arrays.

By using the library function arrayOf, you can define an array.

Ex
val numbers: Array<Int> = arrayOf(10, 20, 30, 40, 50)

ArrayDemo.kt
fun main(args: Array<String>) {

 val numbers: Array<Int> = arrayOf(10, 20, 30, 40, 50)

 for (element in numbers) {
  println(element)
 }

}

Output
10
20
30
40
50

Kotlin provides below library functions to define array of primitive types.

byteArrayOf()
shortArrayOf()
intArrayOf()
longArrayOf()
floatArrayOf()
doubleArrayOf()
CharArray()
BooleanArray()


PrimitiveArrayDemo.kt
fun main(args: Array<String>) {

 var a: ByteArray = byteArrayOf(1, 2, 3)
 val b: ShortArray = shortArrayOf(1, 2, 3)
 val c: IntArray = intArrayOf(1, 2, 3)
 var d: LongArray = longArrayOf(1L, 2L, 3L)
 val e: FloatArray = floatArrayOf(1f, 2f, 3f)
 val f: DoubleArray = doubleArrayOf(1.1, 2.2, 3.3)
 var g: CharArray = charArrayOf('a', 'b', 'c')
 var h: BooleanArray = booleanArrayOf(true, false, true)

 println("Elements of Byte array are : ")
 for (ele in a) {
  println(ele)
 }

 println("\n\nElements of Short array are : ")
 for (ele in b) {
  println(ele)
 }

 println("\n\nElements of Int array are : ")
 for (ele in c) {
  println(ele)
 }

 println("\n\nElements of Long array are : ")
 for (ele in d) {
  println(ele)
 }

 println("\n\nElements of Float array are : ")
 for (ele in e) {
  println(ele)
 }

 println("\n\nElements of Double array are : ")
 for (ele in f) {
  println(ele)
 }

 println("\n\nElements of Char array are : ")
 for (ele in g) {
  println(ele)
 }

 println("\n\nElements of Boolean array are : ")
 for (ele in h) {
  println(ele)
 }

}


Output
Elements of Byte array are :
1
2
3


Elements of Short array are :
1
2
3


Elements of Int array are :
1
2
3


Elements of Long array are :
1
2
3


Elements of Float array are :
1.0
2.0
3.0


Elements of Double array are :
1.1
2.2
3.3


Elements of Char array are :
a
b
c


Elements of Boolean array are :
true
false
true


Previous                                                 Next                                                 Home

Tuesday, 28 November 2017

Kotlin: String template expressions

By using template expression, you can evaluate the expression in a string. Template expression starts with $ symbol followed by a name.

Ex
var a = 10
println("Value of a is : $a")

Arbitrary expression are enclosed in curly braces

var str = "Hello World"
println("$str length is ${str.length}")

Expression.kt

fun main(args: Array<String>) {
 var a = 10
 var str = "Hello World"

 println("Value of a is : $a")
 println("$str length is ${str.length}")
}

Output

Value of a is : 10
Hello World length is 11



Previous                                                 Next                                                 Home

Monday, 27 November 2017

Kotlin: Strings

String is a collection of characters, enclosed in double quotes. Strings in kotlin are defined by using the keyword String and these are immutable.

Ex
var message: String = "Good Morning"

StringsDemo.kt
fun main(args: Array<String>) {
 var message: String = "Good Morning"

 println(message)
}

Output
Good Morning


All the escape sequences (\n, \t, \a etc.,) supported by java are also supported by kotlin.

Ex
var message: String = "Good Morning\nHow are you\nsee you soon"


StringDemo.kt
fun main(args: Array<String>) {
 var message: String = "Good Morning\nHow are you\nsee you soon"

 println(message)

}

Output

Good Morning
How are you
see you soon

If you do not want to perform escaping, then embed the string in """ (triple quotes).


StringDemo.kt

fun main(args: Array<String>) {
 var message: String = """Good Morning\nHow are you\nsee you soon
 
       
       After two lines\bgood bye"""

 println(message)

}

Output

Good Morning\nHow are you\nsee you soon


                                                        After two lines\bgood bye



Previous                                                 Next                                                 Home

Saturday, 25 November 2017

Kotlin: Booleans

Boolean type is used to represent true, false states.

Booleans.kt

fun main(args: Array<String>) {

 var trueState : Boolean  = true
 var falseState : Boolean  = false

 println("Value of trueState is : ${trueState}")
 println("Value of falseState is : ${falseState}")
}

Output

Value of trueState is : true
Value of falseState is : false


Previous                                                 Next                                                 Home