groovy 转置不同大小的二维数组

groovy transpose 2d array different sizes

groovy 我想对列表列表(不同大小)进行转置。

def mtrx = [
   [1,2,3],
   [4,5,6,7]
]

预期结果:

[[1,4],[2,5],[3,6],[null,7]] 

[[1,4],[2,5],[3,6],[7]] 

方法 .transpose() 适用于大小相等的情况下工作正常,但对于不相等的情况 - 一些元素被切断。

我的代码是:

def max = 0
def map = [:]
def mapFinal = [:]
def row = 0

def mtrx = [
   [1,2,3],
   [4,5,6,7]
]

mtrx.each{it->
    println it.size()
    if(max < it.size()){
        max = it.size()
    }
}
def transposed = mtrx.each{it->
   println it
   it.eachWithIndex{it1, index->
       println it1 + ' row ' + row  + ' column ' +index
       mapFinal[row+''+index] = it1
       map[index+''+row] = it1
   }
   row++
}
println map
println mapFinal

尝试

(0..<(mtrx*.size().max())).collect {
    mtrx*.getAt(it)
}