I don’t know any kotlin specific method to do this but you could use java’s System.arraycopy
fun main(args: Array<String>){
//sampleStart
val f = floatArrayOf(1f, 2f, 3f, 4f)
val insert = floatArrayOf(5f, 6f)
System.arraycopy(insert, 0, f, 1, 2)
f.forEach { println(it) }
//sampleEnd
This will however require you to create the insert array. So if this is part of your openGL code you call a lot, you might want to avoid it for performance. Although I don’t think it is that bad, your code above is obviously faster. You also might want to create your own utility function around this. Something like
fun FloatArray.update(startingPoing: Int, vararg newValeus: Float) ...
Much better indeed, it uses block memory copy instead of element by element copy, which is much faster.
The performance loss mentioned by @Wasabi375 could be compensated by creating injecting array once with maximum length and using only part of it.
If performance of this function really is critical I am not sure whether using arraycopy is the right approach though as an additional array has to be allocated and needs to be handled by the gc. I think the original code would still be the most performant, although I guess this would need a lot of benchmarking to be sure and could vary from system to system.