将所有值複制到Set< String>然後使用contains():
Set set = new HashSet (Arrays.asList (stringArray));
while (!set.contains(line)) { ... }
[編輯]如果要查明該行的一部分是否包含該集合中的字元串,則必須循環該集合.将set.contains(line)替換為:
public boolean matches(Set set, String line) {
for (String check: set) {
if (line.contains(check)) return true;
}
return false;
}
使用正規表達式或更複雜的比對方法時,請相應地調整檢查.
[EDIT2]第三個選項是在一個巨大的正規表達式中連接配接數組中的元素|:
Pattern p = Pattern.compile("str1|str2|str3");
while (!p.matcher(line).find()) { // or matches for a whole-string match
...
}
如果數組中有許多元素,這可能會更便宜,因為正規表達式代碼将優化比對過程.