java - 如何在不影响数组中其他值的情况下操作可变 NaturalNumber 数组?
问题描述
下面是带有代码的合约。基本上,如果 的值为,则应nnArray
为。问题是当 被调用时,整个数组将根据. 所以如果是那么将是[1,2,3,4,5,6]
neww
[1,2,6,24,,120,720]
neww[i].multiply(c);
c
c
2
nnArray
[2,2,2,2,2,2]
/*
* Replaces each element of {@code nnArray} with the partial product of all
* the elements in the incoming array, up to and including the current
element.
*
* @param nnArray
* the array
* @updates nnArray
* @requires nnArray.length > 0
* @ensures <pre>
* for all i: integer where (0 <= i < nnArray.length)
* (nnArray[i] = [#nnArray[0] * #nnArray[1] * ... * #nnArray[i]])
* </pre>
*/
private static void computePartialProducts(NaturalNumber[] nnArray) {
assert nnArray != null : "Violation of: nnArray is not null";
assert nnArray.length > 0 : "Violation of: nnArray.length > 0";
NaturalNumber[] neww = new NaturalNumber[nnArray.length];
NaturalNumber count = new NaturalNumber2(1);
for (int i = 0; i < nnArray.length; i++) {
neww[i] = count;
}
for (int i = 0; i < nnArray.length; i++) {
NaturalNumber c = new NaturalNumber2(nnArray[i]);
// neww[i] = c;
neww[i].multiply(c);
}
解决方案
推荐阅读
- python - 在执行以下python理解时面临
- laravel - 如何在 Laravel 中对三列进行分组?
- python - 为什么模型运行时检查点出错?
- pdf - pdf 中的动态 URL 链接
- clips - CLIPS 我如何知道我每天生产哪些产品?
- android - 任务 ':app:mergeDebugResources' 执行失败。com.android.build.gradle.internal.res.ResourceCompilerRunnable
- mysql - 如何计算表格中的多列?
- android - Android-x86 启动时冻结
- pandas - Modin 与 dask 冲突
- javascript - 如何在 Lodash 中将对象数组转换为整数数组