Kotlin studies Chapter 1

Posted by mcirl2 on Thu, 24 Feb 2022 11:06:13 +0100

array

Part I summary review

Previous link address: Kotlin studies Chapter 1 (1)
Knowledge learned in the previous article:

  • Basic data type: includes numeric type and string type
  • Value types: Byte, Int, Long, Float, Double. There is no packing type. Pay attention to the mark of Long type, which must be capitalized with "L"
  • String type: use of string template, comparison of strings, use of * * native string ""... "" "*

array

Similarly, compared with java, it will be easier to remember the old rules. The last table:

kotlinjava
integerIntArrayint [ ]
Packing integerArray< Int >Integer [ ]
characterCharArraychar [ ]
Character boxingArray< Char >Character [ ]
character stringArray< String >String [ ]

Of course, the same Float and Double are the same. They are not listed in the table.

Array creation

Let's take a look at the difference between array creation in java and array creation in kotlin
java

int[] c = new Int[]{1,2,3,4,5};
System.out.println(c.length);

Kotlin

//Two creation methods
//1. Value creation
val c0 = intArrayOf(1,2,3,4,5) 
//2. Expression creation
val c1 = intArray(5){it+1} // Here, 5 represents the size of the array, and it is the corresponding index subscript in the array
println(c1.contentToString()) //Print this array and have a look
println(c1.size)
  • There are still some differences in the way of creation. The "it" here doesn't need to be tangled first, and it will often be seen later. Here it represents the subscript in the array.

  • There is also a contentToString()
    This function is equivalent to a function tailored for the array, which can easily print out the contents of the array. The output result of the above content is: [1,2,3,4,5]. It feels that kotlin has many such functions, which greatly improves the efficiency

  • Get the length of the array. In java, the length of the array is used, while in kotlin, the size is used

Reading and writing of arrays

java

String[] d = new String[]{"Hello","World"};//establish

d[1] = "Java"; //assignment

System.out.println("d[0]"+",""d[1]");//Value

kotlin

val d = arrayOf("Hello","World")//establish

d[1] = "kotlin"//assignment

println("$d[0],$d[1]")//Value

Note: arrays are created in Koltin. Except for unpacked arrays, other arrays are created using arrayOf, including boxed arrays (e.g. array < string >) or custom arrays (e.g. array < person >)

Read value: "[]" on the left of the = sign is the assignment, and "[]" on the right is the value (not necessarily * * = * * sign)

Array traversal

Let's compare it first

java

float[] e = new float[]{1,3,5,7,9};
//First kind
for (int i = 0; i < e.length;i++){
     System.out.println(e[i]);
}
//Second
for(float element : e){
	System.out.println(element);
}

kotlin

val e = floatArrayOf(1f,3f,5f,7f,9f)
//First kind
for(element in e){
	println(element)
}
//Second
e.forEach {
	println(it)
}

Through comparison, we can see that kotlin traverses through foreach, which is more concise than java.

Determine whether an element is in the array

java

float[] e = new float[]{1,3,5,7,9};
//Test 1f in array
for(float element : e){
	if(element == 1f){
		System.out.println("1f exists in variable e");
		break;
	}
}
//Test 1.5f is not included in the array
boolean exists = false;
for(float element : e){
	if(element == 1.5f){
		exists = true;
		break;
	}
}
if(!exists){
	System.out.println("1.5f not exists in variable e");
}

kotlin

val e = floatArrayOf(1f,3f,5f,7f,9f)
//Test 1f in array
if(1f in e){
	println("1f exists in variable e")
}
//Test 1.5f is not included in the array
if(1.5f !in e){
	println("1.5f not exists in variable e")
}

Note: there is a keyword "in" here, which indicates traversal in the foreach loop and inclusion in the condition judgment

After reading, I found that kotlin's judgment is more cumbersome than that of java. It's not a feeling. That's all for today's study.

***

  • Type comparison between array and java
  • Array creation method: value creation and expression creation ({it+1})
  • Access to arrays: read / write, traversal and inclusion of arrays

Topics: Java kotlin