Java 重载一个类的构造函数,每个构造函数都有不同的实例变量

Java 重载一个类的构造函数,每个构造函数都有不同的实例变量,java,constructor,overloading,min,multipleoutputs,Java,Constructor,Overloading,Min,Multipleoutputs,我最初想创建一个“方法”Min,它将搜索数组的Min并返回两个元素。一个表示最小值,另一个表示该值的索引。为此,我决定创建一个类Min和一个构造函数,将这两个元素存储为实例变量,可通过getter和setter访问(一个double和一个int) 棘手的是,我想重载我的类,并添加第二个构造函数,这个构造函数将使用2D数组作为输入,并有两个其他实例变量(double[]和int[]) 它正在工作,但是我感到不舒服,因为我可以创建这个类的实例,例如使用第一个构造函数,并且仍然可以访问对这个构造函数没

我最初想创建一个“方法”Min,它将搜索数组的Min并返回两个元素。一个表示最小值,另一个表示该值的索引。为此,我决定创建一个类Min和一个构造函数,将这两个元素存储为实例变量,可通过getter和setter访问(一个double和一个int)

棘手的是,我想重载我的类,并添加第二个构造函数,这个构造函数将使用2D数组作为输入,并有两个其他实例变量(double[]和int[])

它正在工作,但是我感到不舒服,因为我可以创建这个类的实例,例如使用第一个构造函数,并且仍然可以访问对这个构造函数没有意义的其他两个变量,反之亦然。

我可以为我的第二个构造函数创建一个完全不同的类来解决这个问题(例如:一个类minu array和minu 2Darray),但是我希望它们具有相同的名称,因为它代表相同类型的操作

我相信应该有一种比我选择的方法更优雅的重载和检索多个结果的方法,我很乐意提供一些建议或了解您的最佳做法。谢谢。

我的第二个(更小的)顾虑是,我必须在构造函数2中创建许多构造函数1的实例,这看起来很奇怪,而且在内存分配方面根本没有效率

我的班敏:

package operation;

public class Min {

    // ---------------- Instance Variables ------------------ 
    private int index = 0; 
    private double value = Double.POSITIVE_INFINITY; // important as i search for the MIN 
    private int[] ArrayIndex;   
    private double[] ArrayValue; 


    // ---------------- Constructor 01 ------------------ 
    public Min(double [] anArray) {
        for (int i = 0; i < anArray.length; i++) {
            if (anArray[i] < this.value) {
                this.value = anArray[i]; 
                this.index = i; 
            }
        }
    }

    // ---------------- Constructor 02 ------------------ 
    public Min(double [][] a2DArray, boolean accordingToRow) {

        int n_row = a2DArray.length; 
        int n_col = a2DArray[0].length; 

        if (accordingToRow == true) {
            this.ArrayIndex = new int [n_row]; 
            this.ArrayValue = new double [n_row];
            for (int i = 0; i < n_row; i++) {
                Min minofOneArray = new Min(a2DArray[i]);  // Here i call and create multiple instance of constructor 01.  
                this.ArrayIndex[i] = minofOneArray.getIndex(); 
                this.ArrayValue[i] = minofOneArray.getValue(); 
            }

        }else { // accordingToRow == false (so it according to Column now)
            
            this.ArrayIndex = new int [n_col]; 
            this.ArrayValue = new double [n_col]; 

            //need to loop to extract a column in this case
            double[] tmpArray = new double [n_row]; 

            for (int j = 0; j < n_col; j++) {
                for (int i = 0; i < n_row; i++) {
                    tmpArray[i] = a2DArray[i][j]; 
                }
                Min minofOneArray = new Min(tmpArray); 
                this.ArrayIndex[j] = minofOneArray.getIndex(); 
                this.ArrayValue[j] = minofOneArray.getValue(); 
            }

        }
        
    }

    // ---------------- Getters & Setters ------------------ 
    public int getIndex() {return this.index ; }
    public double getValue() {return this.value ; }

    public int[] getArrayIndex() {return this.ArrayIndex ; }
    public double[] getArrayValue() {return this.ArrayValue ; }

}

package operation_test;

import java.util.Arrays;

import operation.Min;

class Test_MIN {
public static void main(String[] args) {
        
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>> Test of the Min Class <<<<<<<<<<<<<<<<<<<<<<<<<<" );

        
        double [] anArray = { 0,  2 , 1 , -2 , 5 };  
        
        Min minOfArray = new Min(anArray); 
        
        System.out.println(minOfArray.getIndex());
        System.out.println(minOfArray.getValue());
        System.out.println("--------------- End of test 01 -----------------" );
        
        double [][] a2DArray = {{0.2,5,-1},{1,3,0.5}};  
        
        // 0  5 -1
        // 1  3 0.5
        
        
        Min minOf2DArray = new Min(a2DArray, true); // according to row 
       
        System.out.println(Arrays.toString(minOf2DArray.getArrayIndex()));
        System.out.println(Arrays.toString(minOf2DArray.getArrayValue()));
        System.out.println("--------------- End of test 02 -----------------" );
        
        Min minOf2DArray_AccordingToCol = new Min(a2DArray, false); // according to column
           
        System.out.println(Arrays.toString(minOf2DArray_AccordingToCol.getArrayIndex()));
        System.out.println(Arrays.toString(minOf2DArray_AccordingToCol.getArrayValue()));
        System.out.println("--------------- End of test 03 -----------------" );
        

    }
}
>>>>>>>>>>>>>>>>>>>>>>>> Test of the Min Class <<<<<<<<<<<<<<<<<<<<<<<<<<
3
-2.0
--------------- End of test 01 -----------------
[2, 2]
[-1.0, 0.5]
--------------- End of test 02 -----------------
[0, 1, 0]
[0.2, 3.0, -1.0]
--------------- End of test 03 -----------------
package operation;

import utilitary.Tuple;

public class Min {

    // ---------------- Method 01 ------------------ 
    public static Tuple<Integer, Double> Min_Array(double [] anArray) {
        int index = 0; 
        double value = Double.POSITIVE_INFINITY; // important as i search for the MIN 

        for (int i = 0; i < anArray.length; i++) {
            if (anArray[i] < value) {
                value = anArray[i]; 
                index = i; 
            }
        }
        return new Tuple<>(index, value);
    }

    // ---------------- Method 02 ------------------ 
    public static Tuple<int[], double[]> Min_2DArray(double [][] a2DArray, boolean accordingToRow) {

        int[] arrayIndex; 
        double[] arrayValue; 

        int n_row = a2DArray.length; 
        int n_col = a2DArray[0].length; 

        if (accordingToRow == true) {
            arrayIndex = new int [n_row]; 
            arrayValue = new double [n_row];
            for (int i = 0; i < n_row; i++) {

                Tuple<Integer, Double> minofOneArray = Min_Array(a2DArray[i]);  // Here i call method 01 multiple time 
                arrayIndex[i] = minofOneArray.getFirst(); 
                arrayValue[i] = minofOneArray.getSecond(); 
            }

        }else { // accordingToRow == false (so it according to Column now)

            arrayIndex = new int [n_col]; 
            arrayValue = new double [n_col]; 

            //need to loop to extract a column in this case
            double[] tmpArray = new double [n_row]; 

            for (int j = 0; j < n_col; j++) {
                for (int i = 0; i < n_row; i++) {
                    tmpArray[i] = a2DArray[i][j]; 
                }

                Tuple<Integer, Double> minofOneArray = Min_Array(tmpArray);  
                arrayIndex[j] = minofOneArray.getFirst(); 
                arrayValue[j] = minofOneArray.getSecond(); 
            }
        }
        return new Tuple<>(arrayIndex, arrayValue); 
    }

}
包操作;
公共课{
//------------实例变量----------------
私有整数指数=0;
private double value=double.POSITIVE_无穷大;//在搜索最小值时很重要
私有int[]数组索引;
私有双[]数组值;
//--------------建造商01----------------
公共最小值(双[]位){
for(int i=0;i
我的班级考试分:

package operation;

public class Min {

    // ---------------- Instance Variables ------------------ 
    private int index = 0; 
    private double value = Double.POSITIVE_INFINITY; // important as i search for the MIN 
    private int[] ArrayIndex;   
    private double[] ArrayValue; 


    // ---------------- Constructor 01 ------------------ 
    public Min(double [] anArray) {
        for (int i = 0; i < anArray.length; i++) {
            if (anArray[i] < this.value) {
                this.value = anArray[i]; 
                this.index = i; 
            }
        }
    }

    // ---------------- Constructor 02 ------------------ 
    public Min(double [][] a2DArray, boolean accordingToRow) {

        int n_row = a2DArray.length; 
        int n_col = a2DArray[0].length; 

        if (accordingToRow == true) {
            this.ArrayIndex = new int [n_row]; 
            this.ArrayValue = new double [n_row];
            for (int i = 0; i < n_row; i++) {
                Min minofOneArray = new Min(a2DArray[i]);  // Here i call and create multiple instance of constructor 01.  
                this.ArrayIndex[i] = minofOneArray.getIndex(); 
                this.ArrayValue[i] = minofOneArray.getValue(); 
            }

        }else { // accordingToRow == false (so it according to Column now)
            
            this.ArrayIndex = new int [n_col]; 
            this.ArrayValue = new double [n_col]; 

            //need to loop to extract a column in this case
            double[] tmpArray = new double [n_row]; 

            for (int j = 0; j < n_col; j++) {
                for (int i = 0; i < n_row; i++) {
                    tmpArray[i] = a2DArray[i][j]; 
                }
                Min minofOneArray = new Min(tmpArray); 
                this.ArrayIndex[j] = minofOneArray.getIndex(); 
                this.ArrayValue[j] = minofOneArray.getValue(); 
            }

        }
        
    }

    // ---------------- Getters & Setters ------------------ 
    public int getIndex() {return this.index ; }
    public double getValue() {return this.value ; }

    public int[] getArrayIndex() {return this.ArrayIndex ; }
    public double[] getArrayValue() {return this.ArrayValue ; }

}

package operation_test;

import java.util.Arrays;

import operation.Min;

class Test_MIN {
public static void main(String[] args) {
        
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>> Test of the Min Class <<<<<<<<<<<<<<<<<<<<<<<<<<" );

        
        double [] anArray = { 0,  2 , 1 , -2 , 5 };  
        
        Min minOfArray = new Min(anArray); 
        
        System.out.println(minOfArray.getIndex());
        System.out.println(minOfArray.getValue());
        System.out.println("--------------- End of test 01 -----------------" );
        
        double [][] a2DArray = {{0.2,5,-1},{1,3,0.5}};  
        
        // 0  5 -1
        // 1  3 0.5
        
        
        Min minOf2DArray = new Min(a2DArray, true); // according to row 
       
        System.out.println(Arrays.toString(minOf2DArray.getArrayIndex()));
        System.out.println(Arrays.toString(minOf2DArray.getArrayValue()));
        System.out.println("--------------- End of test 02 -----------------" );
        
        Min minOf2DArray_AccordingToCol = new Min(a2DArray, false); // according to column
           
        System.out.println(Arrays.toString(minOf2DArray_AccordingToCol.getArrayIndex()));
        System.out.println(Arrays.toString(minOf2DArray_AccordingToCol.getArrayValue()));
        System.out.println("--------------- End of test 03 -----------------" );
        

    }
}
>>>>>>>>>>>>>>>>>>>>>>>> Test of the Min Class <<<<<<<<<<<<<<<<<<<<<<<<<<
3
-2.0
--------------- End of test 01 -----------------
[2, 2]
[-1.0, 0.5]
--------------- End of test 02 -----------------
[0, 1, 0]
[0.2, 3.0, -1.0]
--------------- End of test 03 -----------------
package operation;

import utilitary.Tuple;

public class Min {

    // ---------------- Method 01 ------------------ 
    public static Tuple<Integer, Double> Min_Array(double [] anArray) {
        int index = 0; 
        double value = Double.POSITIVE_INFINITY; // important as i search for the MIN 

        for (int i = 0; i < anArray.length; i++) {
            if (anArray[i] < value) {
                value = anArray[i]; 
                index = i; 
            }
        }
        return new Tuple<>(index, value);
    }

    // ---------------- Method 02 ------------------ 
    public static Tuple<int[], double[]> Min_2DArray(double [][] a2DArray, boolean accordingToRow) {

        int[] arrayIndex; 
        double[] arrayValue; 

        int n_row = a2DArray.length; 
        int n_col = a2DArray[0].length; 

        if (accordingToRow == true) {
            arrayIndex = new int [n_row]; 
            arrayValue = new double [n_row];
            for (int i = 0; i < n_row; i++) {

                Tuple<Integer, Double> minofOneArray = Min_Array(a2DArray[i]);  // Here i call method 01 multiple time 
                arrayIndex[i] = minofOneArray.getFirst(); 
                arrayValue[i] = minofOneArray.getSecond(); 
            }

        }else { // accordingToRow == false (so it according to Column now)

            arrayIndex = new int [n_col]; 
            arrayValue = new double [n_col]; 

            //need to loop to extract a column in this case
            double[] tmpArray = new double [n_row]; 

            for (int j = 0; j < n_col; j++) {
                for (int i = 0; i < n_row; i++) {
                    tmpArray[i] = a2DArray[i][j]; 
                }

                Tuple<Integer, Double> minofOneArray = Min_Array(tmpArray);  
                arrayIndex[j] = minofOneArray.getFirst(); 
                arrayValue[j] = minofOneArray.getSecond(); 
            }
        }
        return new Tuple<>(arrayIndex, arrayValue); 
    }

}
包装操作测试;
导入java.util.array;
导入操作.Min;
班级测验{
公共静态void main(字符串[]args){

System.out.println(“>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>最小类的测试如果您希望不同的构造函数使用不同的实例变量集设置实例,那么您希望创建不同类的实例。这些实例可以继承具有公共部分的基类


(这虽然很短,但被OP和@user视为一个答案。我创建它是为了将问题从未回答的问题列表中删除。如果创建了更有用或技术上更详细的答案,请告诉我。我不介意删除我的答案。)

如果希望不同的构造函数使用不同的实例变量集设置实例,则需要创建不同类的实例。这些类可以继承具有公共部分的基类


(这虽然很短,但被OP和@user视为一个答案。我创建它是为了将问题从未回答的问题列表中删除。如果创建了更有用或技术上更详细的答案,请告诉我。我不介意删除我的答案。)

我尝试了一些不同的方法,它更适合我表达的担忧:

如果代码对某人有用,我会把它贴在这里

这个想法是使用一个类“tuple”来检索多个输出,因此我不需要传递给构造函数。我仍然可以创建一个唯一的类,将这两个方法作为静态的

然而,我仍然不知道哪种方法是最好的,我想这取决于应用的方式

类元组:

package utilitary;

public class Tuple<T, U> {
    private final T first;
    private final U second;

    public Tuple(T first, U second) {
        this.first = first;
        this.second = second;
    }

    public T getFirst() {
        return this.first;
    }

    public U getSecond() {
        return this.second;
    }

    @Override
    public String toString() { 
        return this.first + " " + this.second;
    }
}
包实用程序;
公共类元组{
私人决赛第一名;
私人决赛U秒;
公共元组(T第一,U第二){
this.first=first;
这个秒=秒;
}
公共T getFirst(){
先把这个还给我;
}
公共U getSecond(){
把这个还给我;
}
@凌驾
公共字符串toString(){
返回this.first+“”+this.second;
}
}
<