7

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!

5 Answers 5

14

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)
            }
         }
Sign up to request clarification or add additional context in comments.

1 Comment

honestly, this clarifies so much that this snippet should go directly to kotlinlang.org/docs ))
3

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

Comments

2

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()

Comments

2

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

Comments

1

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() 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.