有人可以解释这种奇怪的行为吗? [英] Someone can explain this strange if behavior?

查看:99
本文介绍了有人可以解释这种奇怪的行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个检查对象属性是否满足某些要求的方法,如果是,则返回true,否则返回false.但是,即使参数正确,该函数也始终返回false.这是我做过的原始代码和变通办法,并且可以解决问题,但是我仍然不知道问题的根源,因此我将其张贴在这里,看是否有人可以理解问题.

该类包含一个SacVariable类的ArrayList,它也具有isValid()方法.如果arrayList中的所有SacVariable对象均有效,则大佬也有效.在测试过程中,根据情况,SacVariable.isValid()可以完美地返回true或false.但是for循环中的if总是读为false ...


i have a method that checks is the object properties fulfill some requirements, it returns true if yes, false otherwise. But the function is returning always false even if the parameters are correct. Here is the original code and a workaround i made and that did the job, but i still don''t know the origin of the problem, so i posted it here to see if someone can understand the problem.

The Class contains an ArrayList of Class SacVariable, which also has a method isValid(). If all SacVariable objects in the arrayList are valid then the big guy is valid too. During testing the SacVariable.isValid() works perfectly returning true or false according to the case. But the if inside the for loop always read false...


// Original code (does not work because always returns false)
public boolean isValid()
    {
        if (!this.getList().isEmpty())
        {
            for (SacVariable var : this.getList())
            {
                if (!var.isValid())
                {
                    return false;
                }
            }
            return true;        
        }
        return false;
    }


我认为逻辑很明确,但为清楚起见,我将对其进行解释.
如果arrayList为空(size == 0),我什么也不做,立即返回false.
如果arrayList中有内容,我会检查每个内容是否有效,如果至少1为false,那么我会立即返回false.如果for循环结束,则返回true,因为这意味着所有SacVariable对象均有效.




I think the logic is clear but i will explain it for clarity''s sake.
If the arrayList is empty (size==0) i do nothing and return false straight away.
if there is something in the arrayList, i check each one to see if they are valid, if at least 1 is false then i immediately return false. if the for loops ends then i return true because it means all objects SacVariable are valid.



// Workaround that does the job...
public boolean isValid()
    {
        int validConditions = 0;
        if (this.getList().isEmpty())
        {
            return false;
        }

        for (SacVariable var : this.getList())
        {
            if (var.isValid())
            {
                validConditions++;
            }
        }

        if (validConditions == this.getList().size())
        {
            return true;
        }

        return false;
    }



解决方法是相同的,我最初分离条件以提高可读性以希望发现问题,但失败了,所以我决定添加一个计数器,如果有效条件== this.getList().size(),那么我知道所有SacVariables是有效的.它奏效了...为什么没有其他选择呢?它可能是令人难以置信的小或琐碎的事情,但对于我的一生,我看不到它.

SacVariable.isValid()不会始终返回false.我检查了.这里的奥秘在于,尽管该方法确实返回了true,但循环还是被破坏了.

我并不是要别人给我替代代码,我只是想向这里的所有人展示此方法,以查看是否有人发现了代码问题.



The work around is the same, i initially separated the conditions to improve legibility in hope of finding the problem, that failed so i decided to add a counter, if validConditions==this.getList().size() then i know all SacVariables are valid. It worked... why it didn''t the other way? it may be something incredible small or trivial but for the life of me i can''t see it.

SacVariable.isValid() does not return always false. That i checked. The mystery here is that despite the fact that the method is indeed returning true the loop is broken.

I was not asking for people to give me alternative code, I only wanted to show this method to everyone here to see if someone spotted a problem with the code.

推荐答案

我认为这应该可行:
I would think that this should work:
public boolean isValid() {
    if (this.getList().isEmpty()) {
        // There are no items in the list
        return false;
    }

    for (SacVariable var : this.getList()) {
        if (!var.isValid()) {
            // this item is invalid ergo the whole list is invalid
            return false;
        }
    }

    // every item in the list is valid
    return true;
}



如果没有,请尝试以下操作:



If it doesn''t then try this:

public boolean isValid() {
    if (this.getList().isEmpty()) {
        // There are no items in the list
        return false;
    }

    // assume every item in the list is valid
    boolean valid = true;
    for (SacVariable var : this.getList()) {
        if (!var.isValid()) {
            // this item is invalid ergo the whole list is invalid
            valid = false;
            break;
        }
    }
    
    return valid;
}



一旦发现无效的项目,您就不需要继续循环.



You shouldn''t need to continue in the loop once you have found an invalid item.


对我来说,这听起来像SacVariable.isValid()总是返回false.

检查一个.
To me this sounds like SacVariable.isValid() always returns false.

Check that one.


这篇关于有人可以解释这种奇怪的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆