数组

Kotlin Java
整型 IntArray int[]
整型装箱 Array Integer[]
字符 CharArray char[]
字符装箱 Array Character[]
字符串 Array String[]

数组的创建

1 .

//Java中创建;
int[] c = new int[]{1,2,3,4,5};
//Kotlin中创建;
val c0 = intArrayOf(1,2,3,4,5)

val c1 = IntArray(5){it + 1}    //it的值为数组下标,it + 1 表示 下标+1;

数组的长度

1 .

//Java中获取数组长度;
int[] a = new int[5];
System.out.println(a.length);
//Kotlin中获取数组长度;
val a = IntArray(5)
println(a.size)

数组的读写

1 .

//Java中数组的读写;
String[] d = new String[]{"Hello","World"};
d[1] = "Java";
System.out.println(d[0] + "," + d[1]);
//Kotlin中数组的读写;
val d = arrayOf("Hello","World")
d[1] = "Kotlin"
println("${d[0]}","${d[1]}")

数组的遍历

1 .

//Java中数组的遍历;
float[] e = new float[]{1,3,5,7};
for(float element : e){
    System.out.println(element);
}

for(int i = 0;i < e.length;i ++){
    System.out.println(e[i]);
}
//Kotlin中数组的遍历;
val e = floatArrayOf(1f,3f,5f,7f)
for(element in e){
    println(element)
}

e.forEach{element ->
    println(element)
}

数组中值的判断

1 .

//Java中判断某个值是否在数组中;
for(float element : e){
    if(element == 1f){
        System.out.println("1f 存在于数组中");
        break;
    }
}
//Kotlin判断某个值是否在数组中;
val e = floatArrayOf(1f,3f,5f,7f)
if(1f in e){
    println("1f 存在于数组中")
}

//判断某个值不在数组中;
val e = floatArrayOf(1f,3f,5f,7f)
if(1.2f !in e){
    println("1.2f 不存在于数组中")
}

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
----------------last line for now-----------------