嘿大家,
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方法的最后,我有一行代码输出所有这些变量,但我想要它,以便我有一个方法,而不是那行代码。这主要是因为我有多个排序算法,只想简化一切。
答案 0 :(得分:0)
尝试制作这样的课程:
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!
}
答案 1 :(得分:0)
只需制作一个像
这样的私有方法private void printStuff (int tempVar,int swappedValue,int loopExecuted,int comparisonMade){
// print it how ever you want
}