从这种方法出来

时间:2011-03-04 18:31:54

标签: java

我已经编写了下面的代码,如果这个else if语句运行,我想突然从这个方法出来,然后回到这个方法被调用的下一行。我已经使用了返回,但它没有'工作得很好。

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

2 个答案:

答案 0 :(得分:6)

您的代码示例使用break;而非return; - break不会执行此任务。

此外,由于您正在递归:return不会返回多个调用级别。如果要立即退出所有递归调用,则需要向调用者发出此信号。

E.g。

 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;
}

答案 1 :(得分:0)

正如所写,当你不在循环或开关内时,你试图使用breakreturn正常工作,因为您在列表为空时在开头使用它。

您也将noSolution设置为0,即使该块仅在已经等于0时执行。

关于编辑,在命名变量时需要约定。您有两个单大写字母变量。这将使人们认为它是泛型类型,特别是当其中一个是T时。另外,由于您只给我们方法,我们必须假设所有未定义的变量都是类变量,但可能有不明确的类型或状态。