Home > Software design >  How to take an array of integer as variable and print out the first element of the array in kotlin?
How to take an array of integer as variable and print out the first element of the array in kotlin?

Time:06-30

I am new to kotlin

From what I understanding this code should be working but it isn't

fun main(args: Array<Int>) {
   //printing out the first element of the array
    println(args[0])
}

main([12,3,4,5])

CodePudding user response:

The main function is the program entry point and needs to have a specific function signature.

Also, to create an array in Kotlin from hard-coded elements you would use arrayOf(). You can find more about it here.

A working example would be:

fun main()
{
    test(arrayOf(12,3,4,5))
}

fun test(args: Array<Int>) {
    //printing out the first element of the array
    println(args[0])
}
  • Related