小编t-o*_*oto的帖子

Java中返回布尔值的最佳方法

我是一个初学者程序员。

我对返回boolean值的方式有疑问。

你喜欢哪一个?是什么原因?

1。

public boolean methodA(List<String> targetList){
    boolean result = false;         
        for(String str : targetList){

            if(str.equals("word1")) {
                result = true;
                break;
            }
            if(str.equals("word2")) {
                result = true;
                break;
            }
        }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

2。

public boolean methodB(List<String> targetList){
    boolean result = false;         
        for(String str : targetList){

            if(str.equals("word1")) {
                return true;
            }
            if(str.equals("word2")) {
                return true;
            }
        }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

java

2
推荐指数
1
解决办法
257
查看次数

标签 统计

java ×1