重用代码 - Java

Ale*_*and 3 java code-reuse function operator-overloading

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

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;
}
Run Code Online (Sandbox Code Playgroud)

che*_*vim 7

是的,使用http://en.wikipedia.org/wiki/Strategy_pattern,但它可能是一个矫枉过正.重复的迭代和计数++看起来并不那么糟糕.