To create a Three Dimensional Array in Kotlin, you can use the Array constructor with specified dimensions.
In this example,
fun main() {
// Creating a 3D Array
val threeDArray = Array(3) { Array(2) { Array(4) { 0 } } }
// Accessing and modifying elements
threeDArray[1][0][2] = 5
// Printing the array
for (array2D in threeDArray) {
for (array1D in array2D) {
println(array1D.contentToString())
}
}
}
[0, 0, 0, 0] [0, 0, 0, 0] [0, 0, 5, 0] [0, 0, 0, 0] [0, 0, 0, 0] [0, 0, 0, 0]
In this tutorial, we learned How to create a Three Dimensional Array in Kotlin language with well detailed examples.