Kotlin Program to Convert List (ArrayList) to Array

1. Introduction


In this article, you'll learn how to convert List to Array in Kotlin.

This is part of the Kotlin Programming Series. Here you can find more programs in kotlin.

This can be done simply by calling the toArray() method on the collection object.

Kotlin Program to Convert List (ArrayList) to Array



2. Kotlin Program to Convert List (ArrayList) to Array


[package com.javaprogramto.kotlin.convert

import java.util.*

fun main(args: Array<String>){

// creating a ArrayList. var list = ArrayList<String>();

// Adding values list.add("hello");
list.add("world");
list.add("welcome to");
list.add("javaprogramto.com blog")


// creating an emmpty array with size of list. var array = arrayOfNulls<String>(list.size);

println("print empty array as string : "+ Arrays.toString(array));
// converting list to array list.toArray(array);

println("print converted array : "+array);

println("print converted array as string : "+ Arrays.toString(array));

}]
Output:
[print empty array as string : [null, null, null, null]
print converted array : [Ljava.lang.String;@6ff3c5b5
print converted array as string : [hello, world, welcome to, javaprogramto.com blog]]

2.1 Explanation of List to Array in kotlin



This program is similar to the java programming language. But here the trick is when Creating an instance for ArrayList, no need to use "new" keyword in kotlin.

First, Created a List type ArrayList<String>() which stores the String values.

Next, On the list invoke the add(String) method which adds the values to the list directly.

After that first, an empty array should be created with the size of the list. Because you create the array size lesser than list then it will now throw any error or exception. But, it produces a null value array as below. Here, size is passed to array is 1. So, it is generated with one null value.

[print converted array : [Ljava.lang.String;@6ff3c5b5
print converted array as string : [null]]
Therefore, always array size should be as same as List when doing a conversion.

Kotlin base package has a function arrayOfNulls<T>(int size) which takes the size of the array that should be created and it should hold the String type values.

After the array is created then it will have null values in it.

Next, call list.toArray(array) which internally copies all values from list to array.

If you print directly returned array then it prints memory address of the array.

Hence, to see the actual contents inside the array, you should call Arrays.toString() method.

3. Convert List<Integer> to Array in Kotlin

[package com.javaprogramto.kotlin.convert

import java.util.*

fun main(args: Array<String>){

// creating a Integer ArrayList . var list = ArrayList<Integer>();

// Adding values list.add(Integer(1));
list.add(Integer(2));
list.add(Integer(3));
list.add(Integer(4))


// creating an empty intger array with size of list. var array = arrayOfNulls<Integer>(list.size);

// converting list to array list.toArray(array);

println("print converted int array as string : "+ Arrays.toString(array));

}]
Output:
[print converted int array as string : [1, 2, 3, 4]]

Here, Created a List of integers and added 1,2,3,4 values to it. Next, Called toArray() method that converted as an integer array.

4. Kotlin Convert LinkedList to Array


[package com.javaprogramto.kotlin.convert

import java.util.*

fun main(args: Array<String>){

// creating a Integer LinkedList . var list = LinkedList<Long>();

// Adding values list.add(100L)
list.add(200L);
list.add(300L);
list.add(400L)


// creating an empty intger array with size of list. var array = arrayOfNulls<Long>(list.size);

// converting LinkedList to array list.toArray(array);

println("print converted long LinkedList as string : "+ Arrays.toString(array));

}]
Output:
[print converted long LinkedList as string : [100, 200, 300, 400]]


5. Conclusion


In conclusion, We've seen how to convert List to Array. Further, covered examples on ArrayList to array and LinkedList to Array.

[lock]

[View on GitHub ##eye##]

[Download ##file-download##]

[/lock]



Kotlin Convert List to Array

0 Comments