如何在 kotlin 中将整数数组作为变量并打印出数组的第一个元素?

Kha*_*ant 1 kotlin

我是科特林新手

据我了解,这段代码应该可以工作,但事实并非如此

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

main([12,3,4,5])
Run Code Online (Sandbox Code Playgroud)

Hor*_*rea 5

函数main是程序的入口点,需要有特定的函数签名

此外,要在 Kotlin 中从硬编码元素创建数组,您可以使用arrayOf(). 您可以在这里找到更多相关信息。

一个有效的例子是:

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

fun test(args: Array<Int>) {
    //printing out the first element of the array
    println(args[0])
}
Run Code Online (Sandbox Code Playgroud)