9

Please take a look at my 2D-Array-Initialization. The code works.

class World(val size_x: Int = 256, val size_y: Int = 256) {

  var worldTiles = Array(size_x, { Array(size_y, { WorldTile() }) })

    fun generate() {
      for( x in 0..size_x-1 ) {
          for( y in 0..size_y-1 ) {
              worldTiles[x][y] = WorldTile()
          }
      }
  }
}

The problem is that it runs the initialization twice. Basically I want to instantiate the WorldTile-Object in the generate() function. So Line 3 shouldn't call "new WorldTile" there. How can I do that?

Also is that the proper Kotlin way of traversing a 2d-Array?

3
  • 1
    Aren't you already initializing it here: Array(size_x, { Array(size_y, { WorldTile() }) })? Commented Jul 19, 2017 at 19:44
  • Yes and that is what I want to avoid :) I just wish to define the type there, not initialize it. Maybe I need to add a few parameters to WorldTile later, have those calculated in the for-loop. Commented Jul 19, 2017 at 19:49
  • As far as I know you'd need a nullable array for that and then you'd use null instead of WorldTile() in Array(..., { /*here*/}) Commented Jul 19, 2017 at 19:50

4 Answers 4

14

To initialise all to a fixed value:

// A 6x5 array of Int, all set to 0.
var m = Array(6) {Array(5) {0} }

To initialise with a lambda:

// a 6x5 Int array initialise i + j
var m = Array(6) { i -> Array(5) { j -> i + j } }

Another way: Here is an example of initialising a 2D array of Float numbers (3 by 6):

var a = Array(3) { FloatArray(6)} // define an 3x6 array of float numbers
for(i:Int in 0 until a.size) {
    for(j : Int in 0 until a[i].size) {
        a[i][j] = 0f // initialize with your value here.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

13

You can make worldTiles a lateinit property, and do all the initialization in the generate function:

class World(val size_x: Int = 256, val size_y: Int = 256) {

  lateinit var worldTiles: Array<Array<WorldTile>>

  fun generate() {
    worldTiles = Array(size_x, {
        Array(size_y, {
            WorldTile()
        })
    })
  }
}

If you try to access worldTiles before calling generate you will get an exception warning that it hasn't been initialized yet.

Comments

3
val twoDimStringArray= arrayOf(
    arrayOf("first","second"),
    arrayOf("foo"),
    arrayOf("bar","great kotlin")
)
for (i in twoDimStringArray){
    for(j in i){
        println(j)
    }
}

first
second
foo
bar
great kotlin

Comments

1

A bit late but could help to somebody if is working with strings

//init 2d array with a fixed size: 
 var data2 = Array<Array<String>>(2) { arrayOf()}
// fill the 2d array
        data2[0] = arrayOf("123","Freddy x","27")
        data2[1] = arrayOf("124","Elon y","45")

cheers!

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.