Initializing a 2D (multi-dimensional) array in Scala

后端 未结 6 1464
深忆病人
深忆病人 2020-12-24 02:13

It\'s easy to initialize a 2D array (or, in fact, any multidimensional array) in Java by putting something like that:

int[][] x = new int[][] {
        { 3,          


        
6条回答
  •  猫巷女王i
    2020-12-24 02:49

    Personally I'd suck it up and type out (or cut and paste) "Array" a few times for clarity's sake. Include the type annotation for safety, of course. But if you're really running out of e-ink, a quick easy hack would be simply to provide an alias for Array, for example:

    val > = Array
    
    val x: Array[Array[Int]] = >(
      >(3, 5, 7),
      >(0, 4, 9),
      >(1, 8, 6)
    )
    

    You could also provide a type alias for Array if you want to shorten the annotation:

    type >[T] = Array[T]
    
    val x: >[>[Int]] = ...
    

提交回复
热议问题