Data structures 实现递增整数列表的最常用方法是什么?

Data structures 实现递增整数列表的最常用方法是什么?,data-structures,groovy,Data Structures,Groovy,我需要一个数据结构,它是一个整数列表,但每次向其中添加一个整数时,它存储的值是它包含的值加上添加的值的总和 例如: def incrementingList = [] incrementingList.add(5) // now it has 5 incrementingList.add(3) // now it has 5,8 incrementingList.add(2) // now it has 5,8,10 是否有一种groovy方法来实现它,以便它可以像示例中那样随时使用 更新 如

我需要一个数据结构,它是一个整数列表,但每次向其中添加一个整数时,它存储的值是它包含的值加上添加的值的总和

例如:

def incrementingList = []
incrementingList.add(5) // now it has 5
incrementingList.add(3) // now it has 5,8
incrementingList.add(2) // now it has 5,8,10
是否有一种groovy方法来实现它,以便它可以像示例中那样随时使用

更新


如果此列表可能包含0,并且如果最后一个元素是0,那么它应该按最后一个非0元素递增,该怎么办

 incrementingList.add(incrementingList.last() + 5)
检查是否为空:

incrementingList.add((incrementingList.size() > 0 ) ? incrementingList.last() + 5 : 5)
,

输出:

[5, 10, 15]

您可以使用元编程来定义自定义方法:

List.metaClass.plusAdd = { e ->
    delegate.isEmpty() ? delegate.add(e) : delegate.add(delegate.last() + e)
}

def l = []
l.plusAdd(5)
l.plusAdd(3)
l.plusAdd(2)
assert l == [5, 8, 10]
编辑

添加最后一个非零元素的更新:

List.metaClass.plusAdd = { e ->
    if(delegate.isEmpty()) {
        delegate << e
    } else {
        def nonZeros = delegate.findAll { it > 0 }
        delegate << (nonZeros ? nonZeros.last() + e : e)
    }
}

def l = []
l.plusAdd(5)
l.plusAdd(3)
l.plusAdd(2)
assert l == [5, 8, 10]
l = [5, 0]
l.plusAdd(5)
assert l == [5, 0, 10]

l = [1,0]
l.plusAdd(5)
assert l == [1, 0 ,6]
List.metaClass.plusAdd={e->
if(delegate.isEmpty()){
委托0}

学员方法略有不同:

def addToList(incList, val) {
    if (incList.size() > 0) {
        incList << incList.last() + val
    } else {
        incList << val
    }

    return incList
}

def incList = []

addToList(incList, 3)
addToList(incList, 2)
addToList(incList, 5)

println incList
已更新

def addToList(incList, val) {

    if (incList.size() > 0) {
        if (val != 0) {
            incList << val + incList.last()
        } else {
            def addVal = incList.size() > 1 ? (incList[-1] - incList[-2]) : incList[-1]
            incList <<  addVal + incList.last()
        }
    } else {
        incList << val
    }
}

def incList = []

addToList(incList, 5)
addToList(incList, 3)
addToList(incList, 0)
addToList(incList, 5)
addToList(incList, 0)

println incList

建议,基于更新请求(据我所知)

[编辑:“空”案例包含在“无非零”案例中,因此可以简化]

List.metaClass.addAsSum = { e ->
    def nonZero = delegate.reverse().find { it != 0 }

    if (nonZero != null) {
        delegate.add(nonZero + e)
    } else {
        delegate.add(e)
    }
}
测试运行:

def list

// test 0
list = []
list.addAsSum(5)
assert list == [5]

// test 1
list = []
list.addAsSum(5)
list.addAsSum(3)
list.addAsSum(2)
assert list == [5, 8, 10]

// test 2
list = []
list.addAsSum(5)
list.addAsSum(3)
list.addAsSum(0)
list.addAsSum(2)
assert list == [5, 8, 8, 10]

// test 3
list = []
list.addAsSum(0)
list.addAsSum(0)
list.addAsSum(0)
list.addAsSum(2)
list.addAsSum(4)
assert list == [0, 0, 0, 2, 6]

添加一个要求,即在只填充零的列表中,只需添加新元素,这是我的建议:

List.metaClass.fill = { n ->
    delegate ? delegate.add((delegate.reverse().find { it > 0 } ?: 0) + n) : delegate.add(n)
    delegate
}

assert [].fill(1).fill(2).fill(3) == [1, 3, 6]
assert [5, 0].fill(5) == [5, 0, 10]
assert [0, 0].fill(5).fill(5) == [0, 0, 5, 10]

请注意,我在fill()中返回委托这样你就可以按顺序调用它。

你不应该改变像“add”这样的方法的语义,这样的东西很容易有很多可以解释的错误。给它一个明确的名称!你能建议如何根据我的更新调整解决方案吗?明天会有一个结果吗?这将是非常好的,非常感谢的朋友。是的,所以有时在运行它时,我没有发现任何异常:无法从空列表访问last()元素。您能建议如何根据我的更新调整解决方案吗?@AbuMariam:我的更新版本应该适合您的需要。谢谢@Roland,非常感谢。
List.metaClass.addAsSum = { e ->
    def nonZero = delegate.reverse().find { it != 0 }

    if (nonZero != null) {
        delegate.add(nonZero + e)
    } else {
        delegate.add(e)
    }
}
def list

// test 0
list = []
list.addAsSum(5)
assert list == [5]

// test 1
list = []
list.addAsSum(5)
list.addAsSum(3)
list.addAsSum(2)
assert list == [5, 8, 10]

// test 2
list = []
list.addAsSum(5)
list.addAsSum(3)
list.addAsSum(0)
list.addAsSum(2)
assert list == [5, 8, 8, 10]

// test 3
list = []
list.addAsSum(0)
list.addAsSum(0)
list.addAsSum(0)
list.addAsSum(2)
list.addAsSum(4)
assert list == [0, 0, 0, 2, 6]
List.metaClass.fill = { n ->
    delegate ? delegate.add((delegate.reverse().find { it > 0 } ?: 0) + n) : delegate.add(n)
    delegate
}

assert [].fill(1).fill(2).fill(3) == [1, 3, 6]
assert [5, 0].fill(5) == [5, 0, 10]
assert [0, 0].fill(5).fill(5) == [0, 0, 5, 10]