我已经编写了下面的代码,如果这个else if语句运行,我想突然从这个方法出来,然后回到调用这个方法的下一行.我已经使用了返回但是效果不好.
else if (balance == 0 && noSolution == 0) {
noSolution = 0;
return; // it doesn't work.
}
Run Code Online (Sandbox Code Playgroud)
方法:
public <E> void rand_Function(List<E> tree, List<E> array) {
if (array.isEmpty()) {
return;
}
if (array.size() == 1) {
preorder = (List<Element>) new ArrayList<E>(tree);
preorder.addAll((Collection<? extends Element>) array);
for (Element e : preorder) {
e.setLevel(0);
}
E1 = getAverageAccessTime(preorder);
listTwo = new ArrayList<Element>(preorder);
if ((E1 < E) || (rand.nextDouble() <= Math.exp(-(Math.abs(E1 - E)) / 0.5 * T))) {
E = E1;
listOne = listTwo;
} else {
noSolution++;
}
balance--;
System.out.println("running"); // EDITED
if (balance == 0 && noSolution ==1) {
noSolution = 0;
T = 0.95 * T;
} else if (balance == 0 && noSolution == 0) {
System.out.println("running");//EDITED
noSolution = 0;
return;//it doesn't work.
}
} else {
for (int i = 0; i < array.size(); i++) {
//create a list without the ith element
List<E> newList = new ArrayList<E>(array);
newList.remove(i);
//create a list by adding the ith element to beginning
List<E> newBeginning = new ArrayList<E>(tree);
newBeginning.add(array.get(i));
rand_Function(newBeginning, newList);
}
}
}
Run Code Online (Sandbox Code Playgroud)
调用上面方法的函数:
private void function(List<Element> list) {
noSolution = 0;
listOne = list;
T = 5;
balance = 3;
E = getAverageAccessTime(listOne);
for (Element e : listOne) {
e.setLevel(0);
}
rand_Function(emptyList, listOne);
System.out.println("coming out suddenly"); //I want to run this statement when I come out from the method above rand_Function(emptyList, listOne);suddenly!
}
Run Code Online (Sandbox Code Playgroud)
输出将返回,我不指望它:
run:
running
running
running
return
running
running
running
running
running
coming out suddenly
Run Code Online (Sandbox Code Playgroud)
但是我需要这个,我希望这个:
run:
running
running
running
return
coming out suddenly
Run Code Online (Sandbox Code Playgroud)
您的代码示例使用break;not return;- break将无法完成工作.
此外,由于您正在递归:return不会返回多个调用级别.如果要立即退出所有递归调用,则需要向调用者发出此信号.
例如
public <E> boolean rand_Function(List<E> tree, List<E> array) {
if (array.isEmpty()) {
return true;
}
if (array.size() == 1) {
preorder = (List<Element>) new ArrayList<E>(tree);
preorder.addAll((Collection<? extends Element>) array);
for (Element e : preorder) {
e.setLevel(0);
}
E1 = getAverageAccessTime(preorder);
listTwo = new ArrayList<Element>(preorder);
if ((E1 < E) || (rand.nextDouble() <= Math.exp(-(Math.abs(E1 - E)) / 0.5 * T))) {
E = E1;
listOne = listTwo;
} else {
noSolution++;
}
balance--;
System.out.println("running"); // EDITED
if (balance == 0 && noSolution ==1) {
noSolution = 0;
T = 0.95 * T;
} else if (balance == 0 && noSolution == 0) {
System.out.println("running");//EDITED
return false;
}
} else {
for (int i = 0; i < array.size(); i++) {
//create a list without the ith element
List<E> newList = new ArrayList<E>(array);
newList.remove(i);
//create a list by adding the ith element to beginning
List<E> newBeginning = new ArrayList<E>(tree);
newBeginning.add(array.get(i));
if (!rand_Function(newBeginning, newList))
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
658 次 |
| 最近记录: |