30

Is it the easiest way to declare two-dimensional Int array with specified size in Kotlin?

val board = Array(n, { IntArray(n) })
1
  • 1
    this.data = Array(rowCount) { IntArray(colCount) } the semantic changed Commented Jan 17, 2022 at 14:29

3 Answers 3

22

Here are source code for new top-level functions to create 2D arrays. When Kotlin is missing something, extend it. Then add YouTrack issues for things you want to suggest and track the status. Although in this case they aren't much shorter than above, at least provides a more obvious naming for what is happening.

public inline fun <reified INNER> array2d(sizeOuter: Int, sizeInner: Int, noinline innerInit: (Int)->INNER): Array<Array<INNER>> 
    = Array(sizeOuter) { Array<INNER>(sizeInner, innerInit) }
public fun array2dOfInt(sizeOuter: Int, sizeInner: Int): Array<IntArray> 
    = Array(sizeOuter) { IntArray(sizeInner) }
public fun array2dOfLong(sizeOuter: Int, sizeInner: Int): Array<LongArray> 
    = Array(sizeOuter) { LongArray(sizeInner) }
public fun array2dOfByte(sizeOuter: Int, sizeInner: Int): Array<ByteArray> 
    = Array(sizeOuter) { ByteArray(sizeInner) }
public fun array2dOfChar(sizeOuter: Int, sizeInner: Int): Array<CharArray> 
    = Array(sizeOuter) { CharArray(sizeInner) }
public fun array2dOfBoolean(sizeOuter: Int, sizeInner: Int): Array<BooleanArray> 
    = Array(sizeOuter) { BooleanArray(sizeInner) }

And usage:

public fun foo() {
    val someArray = array2d<String?>(100, 10) { null }
    val intArray = array2dOfInt(100, 200)
}
Sign up to request clarification or add additional context in comments.

4 Comments

Can you give some documentation's links? I can found any reference and Idea kotlin plug in seams do not resolve it.
The functions and their source code are in my answer @Micheled'Amico ... they are not part of the stdlib.
Sorry I was too lazy when I reed your answer. .... But I tried it and generic version doesn't work. Even it seams correct and it should return a Array(Array<Something>) at run time I receive java.lang.ClassCastException: [[Ljava.lang.Object; cannot be cast to [[Something; exception. Moreover IMHO a better signature can be array2d(sizeOuter: Int, sizeInner: Int, init: (Int, Int)->INNER). I'll file a question to understand what is wrong your function definition.
You already meet this issue at stackoverflow.com/q/34514861/4101725 ... I open a issue on You Track youtrack.jetbrains.com/issue/KT-10555
12

Currently this is the easiest way, we will extend the standard library with appropriate functions later

3 Comments

Do I have other methods to declare two-dimensional Int array Now?
It's 1.1.3 now. Does it up to date?
Are there any new methods to declare two-dimensional Int array available in 2024?
2

Yes, your given code is the easiest way to declare a two-dimensional array.

Below, I am giving you an example of 2D array initialization & printing.

fun main(args : Array<String>) {
    var num = 100

    // Array Initialization
    var twoDArray = Array(4, {IntArray(3)})
    for(i in 0..twoDArray.size - 1) {
        var rowArray = IntArray(3)
        for(j in 0..rowArray.size - 1) {
            rowArray[j] = num++
        }
        twoDArray[i] = rowArray
    }

    // Array Value Printing
    for(row in twoDArray) {
        for(j in row) {
            print(j)
            print(" ")
        }
        println("")
    }

}

1 Comment

But you don't need var rowArray = IntArray(3). Because the second parameter of Array(...) initializes each array element. So you could do val rowArray = twoDArray[i].

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.