如何正确书写if条件
我有一个关于在循环中编写 if 条件的问题。非正式地,我希望条件按如下方式工作。我将有一个字符串,我想将它与字符串列表进行比较。如果它与列表中的任何值都不匹配,则继续执行 if 语句的主体。如果它确实匹配其中一个值,则跳过 if 语句的主体。这是我的代码(我写的注释只是为了清楚):
Iterator it=row.entrySet().iterator();
Iterator iter=getPrimaryKey.entrySet().iterator();
Map.Entry pairs=(Map.Entry)it.next();
if(!(pairs.getKey().equals(iter.next()))){ //this is the condition. I'm talking about. I want the key to run through all the membres of iter. If it is distinct from all of them, go ahead and execute the code in the if statement.
无论如何,我不知道我做错了什么,所以我有兴趣听到建议。
I have a question about writing the if condition in a loop. Informally, I want the condition to work as follows. I will have a string, and I want to compare it against a list
of strings. If it does not match any of the values in the list, then go ahead and execute the body of the if statement. If it does match one of the values, skip the body of the if statement. Here is my code (I wrote comments just be clear):
Iterator it=row.entrySet().iterator();
Iterator iter=getPrimaryKey.entrySet().iterator();
Map.Entry pairs=(Map.Entry)it.next();
if(!(pairs.getKey().equals(iter.next()))){ //this is the condition. I'm talking about. I want the key to run through all the membres of iter. If it is distinct from all of them, go ahead and execute the code in the if statement.
Anyways, I do not know what I'm doing wrong so I'd be interested in hearing suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我理解正确的话,您将需要设置一个标志(如果找到),然后在遍历整个列表后,使用该标志来确定是否在
while< 之外执行 if 语句的主体/代码> 循环:
If I understand you correctly, you will want to set a flag if it is found, then after the whole list has been traversed, use that flag to determine whether to do the body of the if statement, outside of the
while
loop:假设您希望它执行集合中的任何唯一值:
Assuming you want it to execute any unique value in the set:
在我看来,您想检查“it”迭代器中的字符串是否作为 getPrimaryKey.entrySet() 中的字符串之一存在,因此您可能想直接检查它是否包含在那里。
It sounds to me like you want to check if a string in the "it" iterator is present as one of hte strings in the getPrimaryKey.entrySet(), so you might want to check directly if its contained there.
我假设您引用的
key
是一个String
。我会把你的代码写如下:I assume the
key
you are referring is aString
. I would write your code as following:像这样的事情怎么样......你的迭代器非常令人困惑......
How about something like this... your iterators are very confusing...