简体   繁体   English

Java中的布尔值分配

[英]Boolean value assignment in Java

Apologies for the total noob question, but can anyone explain what's happening to the value of match after the for-each loop has finished in the following method? 为总的问题提出的歉意,但是在以下方法中,for-each循环完成后,谁能解释match的值是什么?

Attempts to compile produce the warning: variable match might not have been initialised . 尝试进行编译会产生警告: variable match might not have been initialised

public void listMatching(String searchString) {
boolean match;

for(String filename : files) {
    if(filename.contains(searchString)) {
        System.out.println(filename);
        match = true;
    }
    else {
        match = false;
    }
}

if(match == false) {
    System.out.println("No matches found for " + searchString);
}
}

First you need to define boolean match = false; 首先,您需要定义boolean match = false;

Also,you need to break from the loop once you found the match , other-wise match status will be over-ridden. 另外,一旦找到匹配项,您就需要从循环中中断,否则, match状态将被覆盖。

 if(filename.contains(searchString)) {
        System.out.println(filename);
        match = true;
        break;
    } // this wil help whether a match is found or not

If you are interested in finding number of matches int counter = 0; 如果您有兴趣查找匹配数,则int counter = 0; if(filename.contains(searchString)) { System.out.println(filename); if(filename.contains(searchString)){System.out.println(filename); match = true; match = true; counter++; 计数器++; } // this wil help to find number of matches } //这将有助于查找匹配数

finally System.out.println("number of matches for"+searchString+" : "+counter); 最终System.out.println("number of matches for"+searchString+" : "+counter);

Here's a fix that will do what you want it to: 这是一个修复程序,它将执行您想要的操作:

public void listMatching(String searchString) {
    boolean match = false; // initialize local variable
    for(String filename : files) {
        if(filename.contains(searchString)) {
            System.out.println(filename);
            match = true;
        }
    }
    if(!match) { // same as 'match == false', just without comparison
        System.out.println("No matches found for " + searchString);
    }
}

Local variables have to be initialized. 局部变量必须初始化。 Only fields get the default value of their type. 只有字段会获得其类型的默认值。

If you reassign match to false in the else block, it would be false after the loop, even if every filename contained searchString except the last one. 如果在else块中将match重新分配为false ,则即使在每个filename包含searchString (最后一个除外)的情况下,循环后也将为false

  1. You need to break the loop when you found your match. 找到匹配项时,您需要打破循环。

  2. you need to initialize your found variable, you could also never run your loop, and then your if condition would look at a non initialized variable, thats what your compiler wants to tell you 您需要初始化找到的变量,也永远无法运行循环,然后if条件将查看未初始化的变量,这就是编译器要告诉您的内容

Initialise the variable to false to avoid the warning in your program. 将变量初始化为false以避免在程序中发出警告。

Also, match is a single variable and depending upon the contents of various files (searching for the same string), you are assigning true or false to the same variable. 另外, match是一个变量,根据各种文件的内容(搜索相同的字符串),您要为同一变量分配true或false。

The final boolean value of match is just the result of the search for the string in the last file of the list of files. match的最终布尔值只是在文件列表的最后一个文件中搜索字符串的结果。

It may files array is empty, so you should set default value for match variable; 它可能是文件数组为空,因此应为match变量设置默认值;

boolean match=false;

for(String filename : files) {
    if(filename.contains(searchString)) {
        System.out.println(filename);
        match = true;
        break;
    }
}

if you need to check that all of files has this search string U can use this code : 如果您需要检查所有文件是否都具有此搜索字符串U可以使用以下代码:

boolean match=files.lenght!=0;

for(String filename : files) {
    if(!filename.contains(searchString)) {
        System.out.println(filename);
        match = false;
        break;
    }
}

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

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