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

发布于 2025-02-11 12:05:50 字数 188 浏览 1 评论 0原文

我是科特林的新手

,我理解该代码应该有效,但不是

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

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

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])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三岁铭 2025-02-18 12:05:50

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

另外,要从硬编码元素中创建一个数组,您将使用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])
}

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])
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文