Carlos Vázquez
Carlos Vázquez

Reputation: 388

Multidimensional array of Objects in Kotlin

I'm new in Kotlin, and I want to create a multi dimensional array of a custom class, with null permitted. Something like that

private var array_map = arrayOf<Array<Obstacle?>>()

...

array_map[1][2] = Obstacle()

How can I do it? Thank you!

Upvotes: 7

Views: 11549

Answers (5)

Victor Alonso Barberan
Victor Alonso Barberan

Reputation: 314

In case you need the index of each element in the constructor of the elements of the array:

Declaration:

var matrix: Array<Array<Obstacle?>>

Instantiation and initialization:

matrix = Array(numRows) { row ->
            Array(numCols) { col ->
                Obstacle(row, col)
            }
         }

Upvotes: 13

Lucas Sousa
Lucas Sousa

Reputation: 352

The approach I always use for this case is:

arr2D = Array(sizeA) { Array(sizeB) { content } } 

Note I replaced the sizes by fields names to illustrate that each number/field represents the width and height length of each dimension of the 2D array.

Also, content should be replaced by the main content you want to fill in each coordinate, in your case seems you aims to setup with Obstacle() instances. If you want fill this content in other moment put null or a quick Any() reference.

In this last case, after creating that you can simply perform to set the itens:

arr2D[1][2] = Obstacle() 

Upvotes: 0

Alexey Romanov
Alexey Romanov

Reputation: 170899

Your code will compile as is. The problem is just that array size can't be changed and arrayOf<Array<Obstacle?>>() creates an empty array, so array_map[1][2] = Obstacle() fails at runtime. (Unless you do array_map = ... somewhere between them. Note that you should prefer val arrayMap, which can't be reassigned, unless you have a specific reason to use var.)

If you want your array to start with nulls, there is arrayOfNulls in the standard library, but it only creates a single-dimensional array, and what you really need is an array of arrays of nulls. You can write a helper function:

inline fun <reified T> matrixOfNulls(n: Int, m: Int) = Array(n) { arrayOfNulls<T>(m) }

private val arrayMap = matrixOfNulls<Obstacle>(5, 5) // example arguments

Upvotes: 1

Madhu Bhat
Madhu Bhat

Reputation: 15253

Not sure if this is what you want, but imagine that Obstacle is a custom class with a field num as below

data class Obstacle(var num: Int){}

A 2D array of the Obstacle object would be as below:

val array: Array<Obstacle?> = arrayOf(Obstacle(123), Obstacle(234))
val arrayOfArray: Array<Array<Obstacle?>> = arrayOf(array)
println(arrayOfArray[0][0]) // would print Obstacle(num=123)
println(arrayOfArray[0][1]) // would print Obstacle(num=234)

So you should be declaring your 2D array as below

val arrayOfArray: Array<Array<Obstacle?>> = arrayOf()

Upvotes: 2

nyarian
nyarian

Reputation: 4375

You can use private var arrayMap: Array<Array<Obstacle?>> = arrayOf(). Just wrap with as much Array<> as you need.

Upvotes: 3

Related Questions