Skip to content

Instantly share code, notes, and snippets.

@sfeatherstone
Last active June 21, 2018 13:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfeatherstone/79dd77329b7d27470e6b1d7f985cd35d to your computer and use it in GitHub Desktop.
Save sfeatherstone/79dd77329b7d27470e6b1d7f985cd35d to your computer and use it in GitHub Desktop.
Exploring different ways to add/concatenate two arrays in Kotlin
//Copy using arraycopy
fun catTwoIntArrays1(array1 :IntArray, array2 :IntArray) : IntArray {
val newArray = IntArray(array1.size + array2.size)
System.arraycopy(array1, 0, newArray, 0 , array1.size)
System.arraycopy(array2, 0, newArray, array1.size , array2.size)
return newArray
}
//Copy using for loops
fun catTwoIntArrays2(array1 :IntArray, array2 :IntArray) : IntArray {
val newArray = IntArray(array1.size + array2.size )
for ((index, value) in array1.withIndex()) {
newArray[index] = value
}
for ((index, value) in array2.withIndex()) {
newArray[index + array1.size] = value
}
return newArray
}
//Copy using the lamda in the constructor
fun catTwoIntArrays3(array1 :IntArray, array2 :IntArray) : IntArray {
return IntArray(array1.size + array2.size,
{ if (it<array1.size) array1[it] else array2[it-array1.size]})
}
//Copy using the lamda in the constructor
inline fun <reified T> catTwoArrays3(array1 :Array<T>, array2 :Array<T>) : Array<T> {
return Array<T>(array1.size + array2.size,
{ if (it<array1.size) array1[it] else array2[it-array1.size]})
}
//Super simple way of making new array
fun catTwoIntArraysSimple(array1 :IntArray, array2 :IntArray) = array1 + array2
inline fun <reified T> catTwoArraysSimple(array1 :Array<T>, array2 :Array<T>) = array1 + array2
//Create a class that mimics an immutable Array<>
class CatTwoArrays<T>(internal val array1 : Array<T>, internal val array2 : Array<T>) {
public operator fun get(index: Int): T {
return if (index < array1.size) array1[index] else array2[index - array1.size]
}
public val size: Int = array1.size + array2.size
internal inner class CatTwoArraysIterator() : Iterator<T> {
internal var position = 0
override fun hasNext(): Boolean = (position >= 0) && (position < array1.size + array2.size)
override fun next() = CatTwoArrays@get(position++)
}
public operator fun iterator(): Iterator<T> = CatTwoArraysIterator()
}
//Tests
fun testResult(array :IntArray) {
val reference = IntArray(7, {it+1})
if (reference.contentEquals(array)) println("Pass") else println("Fail ${array.contentToString()}")
}
inline fun <reified T:Int> testResult(array :Array<T>) {
val reference = Array<Int>(7, {it+1})
if (reference.contentEquals(array)) println("Pass") else println("Fail ${array.contentToString()}")
}
inline fun <reified T:Int> testResult(array :CatTwoArrays<T>) {
val reference = Array<Int>(7, {it+1})
for ((index, value) in array.iterator().withIndex()) {
if (reference[index]!=value) {
println("Fail")
return
}
}
println("Pass")
}
//Main
fun main(args: Array<String>) {
val array1 = intArrayOf(1, 2, 3, 4, 5)
val array2 = intArrayOf(6, 7)
val arrayT1 = arrayOf(1, 2, 3, 4, 5)
val arrayT2 = arrayOf(6, 7)
testResult(array1 + array2 )
testResult(arrayT1 + arrayT2 )
//testResult(array1 + arrayT2 ) //ERROR arrays not same type
testResult(catTwoIntArraysSimple( array1 ,array2 ))
testResult(catTwoArraysSimple( arrayT1 ,arrayT2 ))
testResult(catTwoIntArrays1(array1, array2))
testResult(catTwoIntArrays2(array1, array2))
testResult(catTwoIntArrays3(array1, array2))
testResult(catTwoArrays3(arrayT1, arrayT2))
testResult(CatTwoArrays(arrayT1, arrayT2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment