这是我的第一篇文章。我花了几个小时试图弄清楚如何将我的字符串(即 word
)与我的所有 ArrayList
元素进行比较,检查它们是否相等,不考虑大小写。
这是我的代码。
private static void writeFile(Scanner scanWords, ArrayList<String> wordList,
FileWriter fo, BufferedWriter out) throws IOException {
String word = null;
int count = 0;
boolean contains=false;
String temp = "";
for(int i = 0; i<wordList.size();i++){
while(scanWords.hasNext()){
word= scanWords.next();
if(wordList.get(i).equalsIgnoreCase(word)){
contains = true;
}else{
System.out.println(word);
out.write(word);
//out.write(word);
}
}
}
count++;
out.close();
}
提供的任何帮助都将是令人惊叹的。
最佳答案
首先,转换您的 List<String>
到 HashSet
不区分大小写的字符串 - 即全部小写(使用 String.toLowerCase()
方法)。然后您可以使用Collection.contains()
方法
例如
List<String> strings;
Set<String> lowerCaseStrings = new HashSet<String>(strings.size());
for (String str : strings){
lowerCaseStrings.add(str.toLowerCase());
}
boolean match = lowerCaseString.contains(myStr.toLowerCase());
编辑:建议使用 ArrayList<String>
,但应该是HashSet<String>
- 请参阅下面的评论。
关于java - 比较 ArrayList 中的字符串。区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10766391/