Arrays Groovy,在当前节点后插入节点
我会尽力解释情况的 我有以下db列:Arrays Groovy,在当前节点后插入节点,arrays,groovy,insert,each,Arrays,Groovy,Insert,Each,我会尽力解释情况的 我有以下db列: oid - task - start - end - realstart - realend 我的要求是具有如下输出: oid1 - task1 - start1 - end1 oid2 - task2 - start2 - end2 其中task1是task,task2是task+“real”,start1是start,start2是realstart,end1是end,end2是realend 但是 应始终创建第一行(那些start/end字段从不为
oid - task - start - end - realstart - realend
我的要求是具有如下输出:
oid1 - task1 - start1 - end1
oid2 - task2 - start2 - end2
其中task1是task
,task2是task+“real”
,start1是start
,start2是realstart
,end1是end
,end2是realend
但是
应始终创建第一行(那些start/end
字段从不为空),仅当realstart
和realend
存在时才应创建第二行,这可能不是真的
输入是6个数组(每列一个),输出必须是4个数组,如下所示:
#input oid,task,start,end,realstart,realend
#output oid,task,start,end
我在考虑使用类似于oid的东西。每个都可以,但我不知道如何在当前节点之后添加节点。订单在需求中很重要
如需任何解释,请询问,谢谢 如果您需要一个从一开始就不知道大小的“数组”,您应该使用列表。但在Groovy中,这非常容易使用
下面是一个例子:
final int OID = 0
final int TASK = 1
final int START = 2
final int END = 3
final int R_START = 4
final int R_END = 5
List<Object[]> input = [
//oid, task, start, end, realstart, realend
[ 'oid1', 'task1', 10, 20, 12, 21 ],
[ 'oid2', 'task2', 30, 42, null, null ]
]
List<List> output = [ ]
input.each { row ->
output << [ row[ OID ], row[ TASK ], row[ START ], row[ END ] ]
if ( row[ R_START ] && row[ R_END ] ) {
output << [ row[ OID ], row[ TASK ] + 'real', row[ R_START ], row[ R_END ] ]
}
}
println output
如果您需要一个从一开始就不知道大小的“数组”,则应使用列表。但在Groovy中,这非常容易使用
下面是一个例子:
final int OID = 0
final int TASK = 1
final int START = 2
final int END = 3
final int R_START = 4
final int R_END = 5
List<Object[]> input = [
//oid, task, start, end, realstart, realend
[ 'oid1', 'task1', 10, 20, 12, 21 ],
[ 'oid2', 'task2', 30, 42, null, null ]
]
List<List> output = [ ]
input.each { row ->
output << [ row[ OID ], row[ TASK ], row[ START ], row[ END ] ]
if ( row[ R_START ] && row[ R_END ] ) {
output << [ row[ OID ], row[ TASK ] + 'real', row[ R_START ], row[ R_END ] ]
}
}
println output
在您提出意见并理解您不想(或不能)更改输入/输出数据格式后,下面是另一个解决方案,它可以按照您的要求使用类对数据进行分组,并使其更易于管理:
import groovy.transform.Canonical
@Canonical
class Input {
String[] oids = [ 'oid1', 'oid2' ]
String[] tasks = [ 'task1', 'task2' ]
Integer[] starts = [ 10, 30 ]
Integer[] ends = [ 20, 42 ]
Integer[] realstarts = [ 12, null ]
Integer[] realends = [ 21, null ]
List<Object[]> getEntries() {
// ensure all entries have the same size
def entries = [ oids, tasks, starts, ends, realstarts, realends ]
assert entries.collect { it.size() }.unique().size() == 1,
'The input arrays do not all have the same size'
return entries
}
int getSize() {
oids.size() // any field would do, they have the same length
}
}
@Canonical
class Output {
List oids = [ ]
List tasks = [ ]
List starts = [ ]
List ends = [ ]
void add( oid, task, start, end, realstart, realend ) {
oids << oid; tasks << task; starts << start; ends << end
if ( realstart != null && realend != null ) {
oids << oid; tasks << task + 'real'; starts << realstart; ends << realend
}
}
}
def input = new Input()
def entries = input.entries
def output = new Output()
for ( int i = 0; i < input.size; i++ ) {
def entry = entries.collect { it[ i ] }
output.add( *entry )
}
println output
您可以使用output.oids
,output.tasks
,output.tasks
,output.start
和output.end
从output
对象中获取数组(实际上是列表,但如果在列表中,则调用toArray()
以获取数组)
@Canonical
注释只是让类实现equals、hashCode、toString等等
如果您不理解某些内容,请在评论中提问。在您的评论和理解您不想(或不能)更改输入/输出数据格式后,下面是另一个解决方案,它按照您的要求使用类对数据进行分组并使其更易于管理:
import groovy.transform.Canonical
@Canonical
class Input {
String[] oids = [ 'oid1', 'oid2' ]
String[] tasks = [ 'task1', 'task2' ]
Integer[] starts = [ 10, 30 ]
Integer[] ends = [ 20, 42 ]
Integer[] realstarts = [ 12, null ]
Integer[] realends = [ 21, null ]
List<Object[]> getEntries() {
// ensure all entries have the same size
def entries = [ oids, tasks, starts, ends, realstarts, realends ]
assert entries.collect { it.size() }.unique().size() == 1,
'The input arrays do not all have the same size'
return entries
}
int getSize() {
oids.size() // any field would do, they have the same length
}
}
@Canonical
class Output {
List oids = [ ]
List tasks = [ ]
List starts = [ ]
List ends = [ ]
void add( oid, task, start, end, realstart, realend ) {
oids << oid; tasks << task; starts << start; ends << end
if ( realstart != null && realend != null ) {
oids << oid; tasks << task + 'real'; starts << realstart; ends << realend
}
}
}
def input = new Input()
def entries = input.entries
def output = new Output()
for ( int i = 0; i < input.size; i++ ) {
def entry = entries.collect { it[ i ] }
output.add( *entry )
}
println output
您可以使用output.oids
,output.tasks
,output.tasks
,output.start
和output.end
从output
对象中获取数组(实际上是列表,但如果在列表中,则调用toArray()
以获取数组)
@Canonical
注释只是让类实现equals、hashCode、toString等等
如果您不理解某些内容,请在评论中提问。感谢您提供了有用的解决方案,但在我的例子中,列表的每个属性都是一个数组,长度都相同,因此我总共有6个数组,它的工作原理是否相同?为什么您以如此奇怪的格式获取数据?无论如何,我将编辑答案以尝试提供帮助。感谢您提供了有用的解决方案,但在我的例子中,列表的每个属性都是一个数组,长度都相同,因此我总共有6个数组,它的工作方式是否相同?为什么您会以如此奇怪的格式获取数据?无论如何,我将编辑答案,以尝试提供帮助。