迭代二维异构数组groovy

8xiog9wr  于 2022-11-01  发布在  其他
关注(0)|答案(2)|浏览(142)

我有一个特殊的情况,我正在批量获取以下数据作为二维数组。我的消费者服务需要在每个批处理上执行一些逻辑。我如何迭代以下数组以访问每个子数组的第一个和第三个值

[["TCA","VALID","7cb3b016-0f53-40fc-a6ff-55d2fd7613","38216552696060"], ["TCA","VALID","8540347a-0f6e-4f8c-8e5b-dcadfc8069","36116552696114"], ["TCA","VALID","a2e7a765-3be0-4d0f-9974-ee8708dc44","35716552696108"], ["TCA","VALID","a7ec0957-4787-4bf7-942f-e78b171322","32216552696102"], ["TCA","VALID","adf4a75f-b725-4809-a0b8-8923d1a2a2","33316552696037"]]

我需要为每个子阵列提取“TCA”和“38216552696060”之类的值,并在其上应用逻辑,然后移动到下一个子阵列,直到批处理完成。
我试过了,但是它给出了所有子数组的第一个元素的集合。

println queue.take()*.head()

我是groovy的新手,正在寻求帮助。

emeijp43

emeijp431#

queue.each{ e->
    println e[0] //--> should print first element for each subarray
    println e[3] //--> should print fourth element for each subarray
    //any other logic ...
}

或使用默认变量名-it

queue.each{
    println it[0] //--> should print first element for each subarray
    println it[3] //--> should print fourth element for each subarray
    //any other logic ...
}

或与for:

for(e in queue) {
    println e[0] //--> should print first element for each subarray
    println e[3] //--> should print fourth element for each subarray
    //any other logic ...
}

或经典

for(int i=0; i<queue.size(); i++){
    println queue[i][0] //--> should print first element for each subarray
    println queue[i][3] //--> should print fourth element for each subarray
    //any other logic ...
}
eagi6jfj

eagi6jfj2#

println queue*.getAt([0, -1])

应该只获取每个条目的第一个和最后一个

相关问题