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

时间:2013-09-09 21:30:23

标签: list loops grails groovy

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

我设置了断点并逐步完成了该方法,发现1)从查询返回的数据是正确的,2)当我逐步完成循环时,数据被正确地插入到地图中,3)将地图插入列表时出现故障。我用来将元素插入列表(.add,.push,<<,list [(i)] = map等)的所有方法最终都会覆盖列表中的所有先前元素。

请帮忙。我不知道为什么会这样。希望这对那里的人来说很容易。

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 )
}

我需要allShifts的结果是一个地图列表,其中从Shift查询结果中提取所选数据。我尝试过使用shiftRecords.each和eachWithIndex。在将thisShift映射插入allShifts的点上,任何类型的循环都会出现问题。它不仅仅插入一个地图实例,而是用当前的thisShift地图替换所有列表元素。

2 个答案:

答案 0 :(得分:3)

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
}

每次迭代shiftRecords时,您都需要创建一个新地图。虽然,上面的代码可以在groovy中过度简化,如下所示:

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
}

答案 1 :(得分:3)

尝试:

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 ]
}