scala dynamic multi dimensional mutable arrays like datastructures

前端 未结 3 1588
长情又很酷
长情又很酷 2021-02-06 01:15

Is there any way to build dynamic multi-dimensional arrays in Scala? I know arrays in Scala must be initialized in its sizes and dimensions, so I don\'t want that. The data stru

3条回答
  •  无人及你
    2021-02-06 01:30

    You may create an array of 2 dims dynamically like this:

    val aa : Array[Array[Int]] = Array.ofDim (3, 4)
    

    Well, yes, I see, the size is fixed. How about that:

    val i = random.nextInt (5) + 1
    val j = new GregorianCalendar (). get (Calendar.DAY_OF_WEEK)
    val aa : Array[Array[Int]] = Array.ofDim (i, j)
    

    Yes, it is bound to two dimensions. How would you use an array of previously unknown dimension?

    Well - at least, you can:

    val aa : Array [Int] = Array.ofDim (2)
    

    aa: Array[Int] = Array(0, 0)

    val aaa = Array.fill (3) (aa)         
    

    aaa: Array[Array[Int]] = Array(Array(0, 0), Array(0, 0), Array(0, 0))

提交回复
热议问题