简体   繁体   English

Java包含方法

[英]Java contains method

I have a problem with a contains (...) method from List <...> class. 我对List <...>类中的contains(...)方法有问题。 I'm trying to check if a expression (that is loaded from user input) already exist in a List, but if I entered same name as twice, it said there's nothing same in the List. 我正在尝试检查列表中是否已经存在表达式(从用户输入加载的表达式),但是如果我输入相同的名称两次,它表示列表中没有相同的东西。 Please help, there's source code: 请帮忙,有源代码:

boolean checker;

checker = expressions.contains(line[1]);

if (checker == true) {
    System.err.println("This expression has already been declared!");
    return index;
}

PS: line[1] is a second index in array from main function that stores user-entered line split by whitespaces. PS: line [1]是main函数数组中的第二个索引,该索引存储用户输入的由空格分隔的行。 (First index of line need to be always 'var', and second is any word that cannot be twice in the List) 的第一个指数总要成为“变种”,二是不能在列表中两次任意字)

Your list may not have exact same string as provided in the input which may be due to white spaces. 您的列表可能没有与输入中提供的字符串完全相同的字符串,这可能是由于空格引起的。 Try trimming the input and then call contains 尝试修剪输入,然后调用包含

 checker = expressions.contains(line[1].trim());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM