groovy转置2d数组大小不同

时间:2021-08-19 21:28:07

With groovy I want to make a transpose on list of lists(with different sizes).

使用groovy,我想在列表列表(大小不同)上做一个转置。

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

expected result:

预期结果:

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

or

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

Method .transpose() is working for equal sized is working fine, but for not equal - some elements are cut off.

方法。转置()在大小相同的情况下工作得很好,但是对于不相等的情况——一些元素被切断。

My code is:

我的代码是:

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

1 个解决方案

#1


4  

Try

试一试

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

#1


4  

Try

试一试

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