1

这似乎效率低下或丑陋或出于任何原因,我希望它更优雅

if (a > b) {
    recurseWith(a);
    recurseWith(b);
else {
    recurseWith(b);
    recurseWith(a);
}

在我的代码中 a 和 b 更长,新函数的参数更庞大,而且看起来很蹩脚。有没有办法清理这个?

顺序很重要,因为它们使用全局变量,而且,这段代码可以正常工作。

我正在寻找 Java 中的解决方案

-奥斯汀

4

2 回答 2

0

Will following logic not help here ?

if(a<=b) swap(a,b);

recurseWith(a);
recurseWith(b);
于 2012-11-28T04:01:46.670 回答
-2

I don't have a good answer, but depending on the grossness/longness of the parameters can hide them in a Runnable so you only have to write them once. Your decide whether this is "cleaner". It's going to look worse here, because we're using a and b.

Runnable aRunnable = new Runnable() {
    public void run() {
        recurseWith(a);
    }
}

Runnable bRunnable = new Runnable() {
    public void run() {
        recurseWith(b);
    }
}

if (a > b) {
    aRunnable.run();
    bRunnable.run();
else {
    bRunnable.run();
    aRunnable.run();
}
于 2012-11-28T04:03:06.037 回答