java - 方法的可变输出

标签 java methods

大家好,

public void bubbleSort(boolean radioValue, Integer[] bubbleArray) {
    //declare and initialize variables
    int tempVar = 0;
    int swappedValue = 0;
    int loopExecuted = 0;
    int comparisonMade = 0;

    //if radioValue is true run this if statement
    if (radioValue) {
        //declare boolean true and run while statement as long as boolean is true
        //increase loopExecuted
        boolean swapped = true;
        while (swapped) {
            loopExecuted++;

            //declare boolean as false and run for loop to go over array
            swapped = false;
            for (int i = 1; i < bubbleArray.length; i++) {
                //declare and intialize variable and increase loopExecuted and comparisonMade
                int temp = 0;
                loopExecuted++;
                comparisonMade++;

                //if bubbleArray i-1 is greater than bubbleArray i run this if statement
                if (bubbleArray[i - 1] > bubbleArray[i]) {

                    //make temp value bubbleArray i-1
                    //since bubbleArray i is greater than bubbleArray i-1 swap the values
                    //bubbleArray i is now temp which is bubbleArray i-1
                    //make boolean true and increase swappedValue counter
                    temp = bubbleArray[i - 1];
                    bubbleArray[i - 1] = bubbleArray[i];
                    bubbleArray[i] = temp;
                    swapped = true;
                    swappedValue++;
                }
            }
        }

    }
    //output the stats for bubble sort
    outputArea.append("Bubble Sort \n"
            + "Number of the loop was executed:" + loopExecuted + "\n Number of times a comparison was made" + comparisonMade
            + "\n Number of times a value was shifted" + swappedValue);

}

我的问题是,有什么办法可以将 tempVar、swappedValue、loopExecuted 和 ComparisonMade 的变量传递给另一个方法吗? 例如,在 bubbleSort 方法的末尾,我有一行代码输出所有这些变量,但相反,我想要它,以便我有一个方法可以代替该行代码来执行此操作。这主要是因为我有不止一种排序算法,只是想简化一切。

最佳答案

尝试创建一个这样的类:

class BubbleSortParams{
    private int loopExecuted = 0;
    private int swappedValue = 0;
    private int comparisonMade = 0;
    int getLoopExecuted() {
        return loopExecuted;
    }
    void setLoopExecuted(int loopExecuted) {
        this.loopExecuted = loopExecuted;
    }
    int getSwappedValue() {
        return swappedValue;
    }
    void setSwappedValue(int swappedValue) {
        this.swappedValue = swappedValue;
    }
    int getComparisonMade() {
        return comparisonMade;
    }
    void setComparisonMade(int comparisonMade) {
        this.comparisonMade = comparisonMade;
    }
}

然后,不要打印它们的值,而是设置冒泡排序方法的返回类型 BubbleSortParams 并在方法末尾返回该类型的对象。像这样:

public BubbleSortParams bubbleSort(boolean radioValue, Integer[] bubbleArray) {
    //declare and initialize variables
    int tempVar = 0;
    int swappedValue = 0;
    int loopExecuted = 0;
    int comparisonMade = 0;

    //if radioValue is true run this if statement
    if (radioValue) {
        //declare boolean true and run while statement as long as boolean is true
        //increase loopExecuted
        boolean swapped = true;
        while (swapped) {
            loopExecuted++;

            //declare boolean as false and run for loop to go over array
            swapped = false;
            for (int i = 1; i < bubbleArray.length; i++) {
                //declare and intialize variable and increase loopExecuted and comparisonMade
                int temp = 0;
                loopExecuted++;
                comparisonMade++;

                //if bubbleArray i-1 is greater than bubbleArray i run this if statement
                if (bubbleArray[i - 1] > bubbleArray[i]) {

                    //make temp value bubbleArray i-1
                    //since bubbleArray i is greater than bubbleArray i-1 swap the values
                    //bubbleArray i is now temp which is bubbleArray i-1
                    //make boolean true and increase swappedValue counter
                    temp = bubbleArray[i - 1];
                    bubbleArray[i - 1] = bubbleArray[i];
                    bubbleArray[i] = temp;
                    swapped = true;
                    swappedValue++;
                }
            }
        }

    }

    BubbleSortParams bsp = new BubbleSortParams();
    bsp.setComparisonMade(comparisonMade);
    bsp.setLoopExecuted(loopExecuted);
    bsp.setSwappedValue(swappedValue);
    return bsp;

}

现在您可以用不同的方法对变量执行任何您想要的操作!比如下面这样:

public void myMethod(BubbleSortParams bsp) {
    //now you can access the parameters from your other method
    int swappedValue = bsp.getSwappedValue();
    int loopExecuted = bsp.getLoopExecuted();
    int comparisonMade = bsp.getComparisonMade();
    //do whatever you want with the variables now!
}

关于java - 方法的可变输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48696730/

相关文章:

c# - C#中从基类调用重写的方法

objective-c - 声明和访问 BOOL 类方法

Java:数组逆序排序方法

java - 如何将 int 转换为字符串?

java - Android TabHost 不工作。我没有做错任何事

java - 从 netbeans 7.4 建立 MySQL 数据库连接

c# - 从 datagridview 列表达式调用自定义方法

C++方法签名问题

java - 如何创建全屏宽度 jtoolbar

java - 我可以在Eclipse中定义自定义键盘快捷键吗?