我已經寫了下面的代碼,如果這個else if
語句運行我想從這個方法突然出來,並回到這個方法被調用的下一行。我已經使用return,但它doesn工作不好。從這個方法出來
else if (balance == 0 && noSolution == 0) {
noSolution = 0;
return; // it doesn't work.
}
方法:
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);
}
}
}
是上述方法被調用的函數:
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:
running
running
running
return
running
running
running
running
running
coming out suddenly
但我需要這個出來,我期待這一個:
run:
running
running
running
return
coming out suddenly
我很困惑。如果這是完整的代碼示例,那麼函數將返回給調用方,無論您是否調用break break或什麼都不調用。你能詳細說明嗎? – Pablitorun 2011-03-04 18:39:39
也我編輯了我的帖子對不起:) – user472221 2011-03-04 18:44:01
鑑於顯示的代碼,返回語句無關緊要,該函數將立即返回。發佈的代碼或者不是真正的代碼,或者在問題描述中存在未傳達的內容。你能澄清你的意思是「突然出來」嗎?你能告訴我們會發生什麼,而不是你所期望的嗎? – Erik 2011-03-04 18:46:53