Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 使用循环将地图元素添加到列表中_List_Loops_Grails_Groovy - Fatal编程技术网

List 使用循环将地图元素添加到列表中

List 使用循环将地图元素添加到列表中,list,loops,grails,groovy,List,Loops,Grails,Groovy,我正在尝试用从查询/findAllBy中的对象创建的映射填充列表。。。我总是得到与循环中最后一个贴图相同的贴图列表 我设置了断点并逐步执行了该方法,发现1)查询返回的数据是正确的,2)当我逐步执行循环时,数据正确地插入到映射中,3)将映射插入列表时出现故障。我用于将元素插入列表的所有方法(.add、.push、尝试: LOL。也简化了我的解决方案。:)谢谢你为答案添加了更多GROOVE。我需要所有能得到的建议。tim_yates正确地说,我喜欢baby steps,并指出方法中的错误。OP:“我

我正在尝试用从查询/findAllBy中的对象创建的映射填充列表。。。我总是得到与循环中最后一个贴图相同的贴图列表

我设置了断点并逐步执行了该方法,发现1)查询返回的数据是正确的,2)当我逐步执行循环时,数据正确地插入到映射中,3)将映射插入列表时出现故障。我用于将元素插入列表的所有方法(.add、.push、尝试:


LOL。也简化了我的解决方案。:)谢谢你为答案添加了更多GROOVE。我需要所有能得到的建议。tim_yates正确地说,我喜欢baby steps,并指出方法中的错误。OP:“我们可以简化您的解决方案吗?”我:“是的,请看另一个答案;)”谢谢您的回答。这太简单了,我很尴尬,因为我花了这么长时间才把头发拔出来。我不熟悉Groovy和Grails,有时会把事情弄得过于复杂。@CheddarMonkey,正如你说的,你是Groovy和Grails的新手。你需要知道被接受的答案是如何工作的,而不仅仅是使用它。探索
collect
的工作原理。我会花更多的时间给你相同(或类似)的答案,而不是指出你的错误。但我更喜欢后者。:)@切达尔蒙基从未要求接受我的回答。不管怎样,蒂姆的回答将是对你来说最好的方法。:)
def shiftRecords = Shift.findAllByUserAndStartTimeBetween( userInstance, startDate, endDate )
ArrayList allShifts = new ArrayList()
LinkedHashMap thisShift = new LinkedHashMap()
def size = shiftRecords.size()

for ( int i = 0; i < size; i++ ){
    thisShift["id"] = shiftRecords[(i)].id
    thisShift["user"] = shiftRecords[(i)].user
    thisShift["startTime"] = shiftRecords[(i)].startTime
    thisShift["posCode"] = shiftRecords[(i)].posCode
    thisShift["deptCode"] = shiftRecords[(i)].deptCode
    thisShift["billingIDX"] = shiftRecords[(i)].billingIDX

    Position thisPos = Position.findByPositionCode( thisShift.posCode )
    thisShift["posTitle"] = thisPos.shortTitle
    thisShift["deptTitle"] = thisPos.departmentTitle

    allShifts.add( (i), thisShift )
}
def shiftRecords = Shift.findAllByUserAndStartTimeBetween( 
                               userInstance, startDate, endDate )
ArrayList allShifts = new ArrayList()
def size = shiftRecords.size()

for ( int i = 0; i < size; i++ ){
    LinkedHashMap thisShift = new LinkedHashMap()
    thisShift["id"] = shiftRecords[(i)].id
    thisShift["user"] = shiftRecords[(i)].user
    thisShift["startTime"] = shiftRecords[(i)].startTime
    thisShift["posCode"] = shiftRecords[(i)].posCode
    thisShift["deptCode"] = shiftRecords[(i)].deptCode
    thisShift["billingIDX"] = shiftRecords[(i)].billingIDX

    Position thisPos = Position.findByPositionCode( thisShift.posCode )
    thisShift["posTitle"] = thisPos.shortTitle
    thisShift["deptTitle"] = thisPos.departmentTitle

    allShifts << thisShift
}
def shiftRecords = Shift.findAllByUserAndStartTimeBetween( 
                               userInstance, startDate, endDate )
def allShifts = []

shiftRecords.each{
    def thisShift = [:]
    thisShift.id = it.id
    thisShift.user = it.user
    thisShift.startTime = it.startTime
    thisShift.posCode = it.posCode
    thisShift.deptCode = it.deptCode
    thisShift.billingIDX = it.billingIDX

    Position thisPos = Position.findByPositionCode( thisShift.posCode )
    thisShift.posTitle = thisPos.shortTitle
    thisShift.deptTitle = thisPos.departmentTitle

    allShifts << thisShift
}
def shiftRecords = Shift.findAllByUserAndStartTimeBetween( userInstance, startDate, endDate )
def allShifts = shiftRecords.collect { it ->
    def pos = Position.findByPositionCode( it.posCode )
    [ id         : it.id,
      user       : it.user,
      startTime  : it.startTime,
      posCode    : it.posCode,
      deptCode   : it.deptCode,
      billingIDX : it.billingIDX,
      posTitle   : pos.shortTitle,
      deptTitle  : pos.departmentTitle ]
}