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

后端 未结 6 1452
深忆病人
深忆病人 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条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 02:44

    glancing through the answers, i did not find what to me seems the most obvious & simple way to do it. instead of Array, you can use a tuple.
    would look something like that:

    scala> val x = {(
         | (3,5,7),
         | (0,4,9),
         | (1,8,6)
         | )}
    
    x: ((Int, Int, Int), (Int, Int, Int), (Int, Int, Int)) = ((3,5,7),(0,4,9),(1,8,6))
    

    seems clean & elegant?
    i think so :)

提交回复
热议问题