我已经有一段时间没有编程了,所以有点生疏了。我已经被这个问题困扰了几个小时,我想知道在java中有哪些选项可以选择性地返回一些东西(或者在满足特定条件时跳回调用方法-基本上doBranch方法使用parseStatementList处理语句以及其他事情。在过去的几个小时里,我以不同的方式更改了parseStatementList,但它没有任何意义,这可能就是它不能工作的原因。
调用代码:基本上结束,诸如此类,BOO在SKIP_START_SET中。然而,parseStatement将这些作为普通单词来处理,因此如果BOO在字符串或某种类型的列表中,它会在第一个doBranch中丢失。
private C1 parseSelectStatement( TokenSet recoverSet) {
C2 selectCond = doBranch( recoverSet );
while (tokens.isMatch(BLAH)) {
match(BLAH)
C2 result = doBranch ( recoverSet );
}
if (tokens.isMatch(BOO)) {
match(boo)
result1 = doBranch( recoverSet );
}
match( END )
return new StatementNode.SelectNode(selectCond, result, result1);
}
private C2 doBranch(TokenSet recoverSet) {
ExpNode cond = parseCondition(recoverSet.union(Token.KW_THEN));
// The code jumps to below if words in SKIP_START_SET are found in the statement,
// otherwise it will complete and return the new object.
StatementNode result = parseStatementList(recoverSet.union(SKIP_START_SET));
if (tokens.isIn(SKIP_START_SET)) {
return null;
}
return new C2(pos, cond, result);
}发布于 2015-04-13 22:09:40
你可以使用注释和其他答案中声明的,或者如果你想避免返回null,你可以返回一个对象列表,如果你没有返回任何结果,这个列表将是空的:
private List<ClassOfSomeSort> doBranch(TokenSet recoverSet) {
ExpNode cond = parseCondition(recoverSet.union(Token.KW_THEN));
List<ClassOfSomeSort> myResult= new ArrayList<ClassOfSomeSort>();
StatementNode result = parseStatementList(recoverSet.union(SKIP_START_SET));
//if the condition is met you will have a result otherwise it will be an empty list
if (!tokens.isIn(SKIP_START_SET)) {
myResult.add(new ClassOfSomeSort(pos, cond, result));
}
return myResult; //always return this list
}https://stackoverflow.com/questions/29606768
复制相似问题