Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 修复冗余的方法_Java_Methods_Redundancy - Fatal编程技术网

Java 修复冗余的方法

Java 修复冗余的方法,java,methods,redundancy,Java,Methods,Redundancy,我有几个使用数组的方法,正如您所看到的,很多代码都是重复的,唯一真正的区别是每种方法的基本类型都有所改变。我试图找出一个方法,我可以使持有重复的代码,但没有成功 public class Stat { // Private double array data private double data[]; // Default constructor that creates a double array having a single element of 0.0 public Stat

我有几个使用数组的方法,正如您所看到的,很多代码都是重复的,唯一真正的区别是每种方法的基本类型都有所改变。我试图找出一个方法,我可以使持有重复的代码,但没有成功

public class Stat {

// Private double array data

private double data[];

// Default constructor that creates a double array having a single element of 0.0

public Stat() {

    data = new double[1];

    data[0] = 0.0;

} // End of method

/*
 * Constructs a Stat object of type double using the values held in d. The constructor
 * then creates a double array of the same length as d and holds copies of the values of d. A
 * reference to this new array is assigned to data. 
 */

public Stat(double[] d) {

    if (d == null) {

        for (int c = 0; c < data.length; c++) {

            data[c] = 0;

        } // End of for loop

    } // End of if condition

    else {

        data = new double[d.length];

        for (int c = 0; c < data.length; c++) {

            data[c] = d[c];

        } // End of for loop

    } // End of else statement

} // End of method

/*
 * Constructs a Stat object of type float using the values held in f. The constructor
 * then creates a double array of the same length as f and holds copies of the values of f. A
 * reference to this new array is assigned to data.
 */

public Stat(float[] f) {

    if (f == null) {

        for (int c = 0; c < data.length; c++) {

            data[c] = 0;

        } // End of for loop

    } // End of if condition

    else {

        float[] data = new float[f.length];

        for (int c = 0; c < data.length; c++) {

            data[c] = (float) f[c];

        } // End of for loop

    } // End of else condition

} // End of method

/*
 * Constructs a Stat object of type int using the values held in c. The constructor
 * then creates a double array of the same length as c and holds copies of the values of c. A
 * reference to this new array is assigned to data.
 */

public Stat(int[] i) {

    if (i == null) {

        for (int c = 0; c < data.length; c++) {

            data[c] = 0;

        } // End of for loop

    } // End of if condition

    else {

        int[] data = new int[i.length];

        for (int c = 0; c < data.length; c++) {

            data[c] = (int) i[c];

        } // End of for loop

    } // End of else condition

} // End of method

/*
 * Constructs a Stat object of type long using the values held in lo. The constructor
 * then creates a double array of the same length as lo and holds copies of the values of lo. A
 * reference to this new array is assigned to data.
 */

public Stat(long[] lo) {

    if (lo == null) {

        for (int c = 0; c < data.length; c++) {

            data[c] = 0;

        } // End of for loop

    } //End of if condition

    else {

        long[] data = new long[lo.length];

        for (int c = 0; c < data.length; c++) {

            data[c] = (long) lo[c];

        } // End of for loop

    } //End of else condition

} // End of method
公共类统计{
//专用双数组数据
私人双重数据[];
//默认构造函数,用于创建具有单个元素0.0的双数组
公共统计(){
数据=新的双精度[1];
数据[0]=0.0;
}//方法结束
/*
*使用d中保存的值构造double类型的Stat对象
*然后创建一个与d长度相同的双数组,并保存d.a值的副本
*对该新数组的引用已分配给数据。
*/
公共统计(双[]d){
如果(d==null){
for(int c=0;c
这就去掉了一些样板文件

但是,您可能需要检查大型阵列的性能。这会执行很多装箱/拆箱操作,这些操作可能会得到优化,也可能不会

class Stat {
    private final double[] data;

    public Stat() {
        data = new double[1];
    }

     public Stat(int[] iData) {
         if (iData != null) {
             data = copy(iData.length, i -> (double)iData[i]);
         } else {
             data = new double[1];
         }
     }

     public Stat(float[] fData) {
         if (iData != null) {
             data = copy(iData.length, i -> (double)fData[i]);
         } else {
             data = new double[1];
         }
     }

     private double[] copy(int length, Function<Integer,Double> access) {
        double[] data = new double[length];
        for (int i = 0; i < length; i++) {
            data[i] = access.apply(i);
        }
        return data;
    }
}
类统计{
私人最终双[]数据;
公共统计(){
数据=新的双精度[1];
}
公共统计(int[]iData){
如果(iData!=null){
数据=拷贝(iData.length,i->(double)iData[i]);
}否则{
数据=新的双精度[1];
}
}
公共统计(浮动[]fData){
如果(iData!=null){
数据=拷贝(iData.length,i->(double)fData[i]);
}否则{
数据=新的双精度[1];
}
}
私有双[]副本(整数长度,函数访问){
双精度[]数据=新的双精度[长度];
for(int i=0;i
为什么要接受不同类型的数组?我的第一个倾向是只接受
double[]
。如果用户有其他类型的数组,那么他们可以将其转换为
double[]
他们自己,不需要这门课去做。@JohnKugelman这就像是我的一个计算机科学实验课的一个特定部分,我的教授出于某种原因希望这样做,我不知道为什么。如果这也有帮助的话,我可以在它之前发布代码。我同意,只有当你去c时,接受double才更有意义无论如何都要将元素加倍。在任何情况下,您都可以使用Arrays.fill(myArray,0)将数组填充为零,而不是执行for循环。@dustinroepsch,这样就没有方法可以减少冗余,因为这些方法接受不同的类型?顺便说一句,原始数组被初始化为零(或false)如果您想避免自动(取消)装箱,请使用一个。如果您通过了例如长度的
-1
,您还可以通过让
copy
返回
new double[1]
来减少一些代码重复。因此,您可以执行
data=copy(data==null?-1:data.length)
。您甚至可以将
数据==null?-1:data.length
移动到它自己的方法。