kotlin数组

1、kotlin中的数组与java数组比较:

2、创建

Kotlin 复制代码
fun main() {
    // 值创建
    val a = intArrayOf(1,2,3)
    // 表达式创建
    val b = IntArray(3){
        println("it: ${it}")
        it+1
    }
    println("a数组:${a.contentToString()}, 长度:${a.size}")
    println("b数组:${b.contentToString()}, 长度:${b.size}")
}

结果:

3、读写/循环

Kotlin 复制代码
fun main() {
    val a = intArrayOf(1,2,3)
    // 取值
    val b = a[0]
    println("b:${b}")
    // 赋值
    a[0] = 2
    println("a: ${a.contentToString()}")
    // 循环
    for (i in a) {
        println(i)
    }
    // 包含/不包含
    if(1 !in a) {
        println("1 not in a")
    }
    if(2 in a) {
        println("2 in a")
    }
}

4、总结

相关推荐
奔跑吧 android1 小时前
【android bluetooth 协议分析 07】【SDP详解 2】【SDP 初始化】
android·bluetooth·aosp15·bt·gd·sdp_init
梦否4 小时前
Android 代码热度统计(概述)
android
Sylvia-girl4 小时前
Java——抽象类
java·开发语言
Yana.nice6 小时前
Bash函数详解
开发语言·chrome·bash
xchenhao7 小时前
基于 Flutter 的开源文本 TTS 朗读器(支持 Windows/macOS/Android)
android·windows·flutter·macos·openai·tts·朗读器
coder_pig8 小时前
跟🤡杰哥一起学Flutter (三十五、玩转Flutter滑动机制📱)
android·flutter·harmonyos
tomorrow.hello8 小时前
Java并发测试工具
java·开发语言·测试工具
晓13138 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊8 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
消失的旧时光-19439 小时前
OkHttp SSE 完整总结(最终版)
android·okhttp·okhttp sse