Created
March 27, 2024 12:25
-
-
Save enshahar/ef88302c5e6a2d6711ef976c992805a7 to your computer and use it in GitHub Desktop.
Array2d in Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array2D<T>(val n:Int, val m:Int, private val arr:Array<T>) { | |
init { | |
require(n*m == arr.size) | |
} | |
companion object { | |
inline fun <reified T> array2d(n:Int, m:Int, v:T) = | |
Array2D<T>(n,m,Array<T>(n*m){v}) | |
inline fun <reified T> array2d(n:Int, m:Int, block:(Int,Int)->T) = | |
Array2D<T>(n,m,Array<T>(n*m){ i -> | |
val row = TODO() | |
val col = TODO() | |
block(row,col) | |
}) | |
} | |
operator fun get(i: Int, j:Int): T = TODO() | |
operator fun set(i: Int, j:Int, v:T): Unit = TODO() | |
} | |
fun main() { | |
var arr1 = Array2D.array2d(3,3,0.0) | |
arr1[0,0] = 10.0 | |
println(arr1[0,0]) | |
var arr2 = Array2D.array2d(2,3){ i,j -> | |
"$i,$j" | |
} | |
arr2[1,2] = "test" | |
println(arr2[1,2]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment