gpt4 book ai didi

java - 重用代码 - Java

转载 作者:搜寻专家 更新时间:2023-11-01 04:03:38 25 4
gpt4 key购买 nike

有没有办法通过这些函数中的数组代码重用迭代:

public static double[] ln(double[] z) {
// returns an array that consists of the natural logarithm of the values in array z
int count = 0;

for (double i : z){
z[count] = Math.log(i);
count += 1;
}
return z;
}


public static double[] inverse(double[] z) {
// returns an array that consists of the inverse of the values in array z
int count = 0;

for (double i : z){
z[count] = Math.pow(i,-1);
count += 1;
}
return z;
}

最佳答案

是的,使用 http://en.wikipedia.org/wiki/Strategy_pattern但这可能有点矫枉过正。重复迭代和 count++ 看起来还不错。

关于java - 重用代码 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4725077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com