Reputation: 488
I'm trying to read a list of string from a file, which is structured as a list:
ElemA
ElemB
ElemC
I need to save into this variable, which is defined as:
private var history: Array<out String>?
I made this method, but it doesn't works because it requires an Array? as output, but it founds an Array<(out) Any!>!
private fun loadHistory(): Array<out String>? {
val list = ArrayList<String>()
File("history").forEachLine { list.add(it) }
return list.toArray()
}
How can I solve?
Upvotes: 1
Views: 3569
Reputation: 4060
As suggested by @jsamol in the comments.
You should use toTypedArray()
instead of toArray()
to get an array of the specific type.(ref)
toArray()
returns new array of type Array<Any?>
. (ref)
private fun loadHistory(): Array<out String>? {
val list = ArrayList<String>()
File("history").forEachLine { list.add(it) }
return list.toTypedArray()
}
Upvotes: 1