Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - Convert Collection to Array List

I'm converting a Java application to Kotlin. In one area it's using apache IO's FileUtils listFiles functions.

These return collections and I'm having problems converting/casting the collections into ArrayList

        val myFiles = FileUtils.listFiles(mediaStorageDir, extensions, true) as ArrayList<File> 

Whilst this compiles I get a runtime error as follows:

java.util.LinkedList cannot be cast to java.util.ArrayList

What's the correct way to convert a collection object into an ArrayList?

like image 968
Chris Nevill Avatar asked Jan 09 '19 10:01

Chris Nevill


People also ask

Is Kotlin list an ArrayList?

In Kotlin, the default implementation of List is ArrayList which you can think of as a resizable array.


1 Answers

You can easily create ArrayList in Kotlin from any Collection

val collection = FileUtils.listFiles(mediaStorageDir, extensions, true) val arrayList = ArrayList(collection) 
like image 141
Abu Yousuf Avatar answered Sep 28 '22 22:09

Abu Yousuf