Functions with mutable collections

Create a mutable list in the getMutableList function and return the list, as follows:

fun getMutableList() : MutableList<Int> {
val list = mutableListOf(1,2,3,4,5)
return list
}

Create a listFromKotlin variable in Java and assign a list to this variable by using the CallKotlinKt.getMutableList function from Kotlin.

See the following example:

public static void main(String args[]) {

System.out.print("Kotlin mutable list");
//List<int> listFromKotlin = KotlinToJavaKt.mutableList();

List<Integer> listFromKotlin = CallKotlinKt.getMutableList();
listFromKotlin.add(6);
for (int i = 0; i < listFromKotlin.size(); i++) {
System.out.println("Element " + listFromKotlin.get(i));
}
}

Notice that Kotlin is not familiar with primitive data types. As a result, we must provide a list of Integer classes:

//List<int> listFromKotlin = KotlinToJavaKt.mutableList(); // list of int
List<Integer> listFromKotlin = CallKotlinKt.getMutableList(); // List of Integers
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.242.235