开发者

Need help changing a reserved word boolean into a HashSet

I have this code in a reserved word boolean format:

private boolean isIdent(String t) {
        if (equals(t, "final") || equals(t, "int") || equals(t, "while")
            || equals(t, "if") || equals(t, "else") || equal开发者_如何学运维s(t, "print")) return false;
        if (t!=null && t.length() > 0 && Character.isLetter(t.charAt(0))) return true;
        else return false;
    }

I need to turn this into a HashSet format but unsure how to approach this. Any help would be most appreciated.


You mean by putting the reserved words in a Set?

private Set<String> keywords;

private void initKeywords() {
    keywords = new HashSet<String>();
    keywords.add("final");
    keywords.add("int");
    keywords.add("while");
    keywords.add("if");
    keywords.add("else");
    keywords.add("print");     
}

private boolean isIdent(String t) {
    if (keywords.contains(t)) {
        return false;
    }
    else if (t != null && t.length() > 0 && Character.isLetter(t.charAt(0))) {
        return true;
    }
    else {
        return false;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜