日期:2014-05-20  浏览次数:20734 次

请帮忙详细解释一下下面的代码,谢谢
代码如下:
import java.util.Stack;
import java.util.Vector;

public class Intermediate {
static String[] symbol = { "+", "-", "*", "/", "" };
static String[] num1 = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
static Vector<String> strVector; // 存储所有表达式

/**
 * 穷举出所有的表达式
 * 
 * @param prevStr
 * @param length
 */
public static void hundred(String prevStr, int length) {
// 加入下一个数字
if (length <= num1.length) {
prevStr += num1[length - 1];
}
for (int i = 0; i < symbol.length; i++) {
// 如果还没到第九个数字,加入下一个符号,继续递归
if (length < num1.length) {
String nextStr = prevStr + symbol[i];
hundred(nextStr, length + 1);
}else {// 如果已经到第九个数字,加入到strVector中
strVector.addElement(prevStr);
break;
}
}
}
public static void main(String[] args) {
if (strVector == null) {
strVector = new Vector<String>();
} else {
strVector.removeAllElements();
}

String prevStr = "";
hundred(prevStr, 1);
}
}

代码的作用是用+、-、*、/等符号,插入到123456789中,列出所有可能的式子放入到strVector中,请详细解释下hundred方法的运行。谢谢。
------解决方案--------------------
递归循环,上面有注释,如,1+2+3+4+5+6+7+8+9