Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
In your situation, the most obvious thing to do is use a for loop over the Iterable or the Iterable.forEach extension method depending on your preference, and directly put floats into the FloatBuff...
Answer
#1: Initial revision
In your situation, the most obvious thing to do is use a for loop over the `Iterable` or the [`Iterable.forEach` extension method](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/for-each.html) depending on your preference, and directly `put` floats into the `FloatBuffer`. This avoids any intermediate data structure. Indeed, I'd do this directly over `myList` to avoid the 3-element list intermediates. ```kotlin for ((x, y, z) in myList) { myFloatBuffer.put(x) myFloatBuffer.put(y) myFloatBuffer.put(z) } ``` As far as creating a `FloatArray` from an `Iterable`/`Iterator`, I think I ran into a similar annoyance last time I was using Kotlin, which was admittedly a while ago. I don't think there's a nice, efficient way to do this in the standard library, though I could have missed something. It's understandable why it is this way; there's no way to know a priori how many elements an `Iterator`/`Iterable` has and `FloatArray`s can't be resized. The standard library option is thus along the lines that you went: convert to a dynamically sized data structure whose size can be measured, namely a `List`, and then convert that to an array. If you do know what the size of the array is, then you have a couple of options. One option is to create the array and then mutate it after the fact: ```kotlin val myFloatArray = FloatArray(knownSize) myIterable.forEachIndexed { i, x -> myFloatArray[i] = x } // or myIterator.withIndex().forEach { (i, x) -> myFloatArray[i] = x } ``` Obviously, this will lead to an array out of bounds exception if there are more than `knownSize` elements in the `Iterable`/`Iterator`. If there are fewer, then the remaining elements of the array will be populated with the default value of 0 (or you could specify a different default to the `FloatArray` constructor). A slightly more efficient but abusive option would be: ```kotlin val myFloatArray = FloatArray(knownSize) { myIterator.nextFloat() } ``` In this case, if `myIterator` has more than `knownSize` elements, they will simply be left unconsumed. If `myIterator` has too few elements, you'd get a `NoSuchElementException`. If performance isn't a concern, the `toList().toFloatArray()` approach is likely the most concise and idiomatic.