尽管条件不相等,但字符串被添加到ArrayList

时间:2015-06-10 16:09:41

标签: java arraylist

当找到一个特殊字符串direcList时,我将firstDirec分为两个子列表secondtDirecNew direction

然后我将特定字符串从列表中过滤掉,而不是将它们添加到新的ArrayList s timeListFirst and timeListSecond中。一切都适用于firstDirec,并且元素未添加到timeListFirst,但我在此位置遇到secondtDirec列表的问题:

if (!(mergeLine.equals(otherDirection))

因为此位置mergeLine等于"Ravensbusch",所以otherDirection也是timeListSecond。尽管如此,这个字符串仍被添加到 for (String keyLine : direcList) { if (keyLine.startsWith("New direction")) { int index = direcList.indexOf(keyLine); // direcArray.remove(index); List<String> firstDirec = direcList.subList(0, index); List<String> secondtDirec = direcList.subList(index, direcList.size() - 1); // This part wih New // Direction. ArrayList<String> timeListFirst = new ArrayList<String>(); for (String mergeLine : firstDirec) { if (!(mergeLine.equals(direction)) && !(mergeLine.equals(route)) && !(mergeLine.equals(day))) { timeListFirst.add(mergeLine); } } ArrayList<String> timeListSecond = new ArrayList<String>(); for (String mergeLine : secondtDirec) { if (!(mergeLine.equals(otherDirection)) && !(mergeLine.equals(route)) && !(mergeLine.equals(day)) && !(mergeLine.equals("New direction"))) { timeListSecond.add(mergeLine); } } 。有人可以解释我的错误吗?

mergeLine值:

enter image description here

otherDirection值:

enter image description here

var
    startTime = Date.now();
    html = "",
    includes = ["header.html","content.html","footer.html"],
    filesLoaded = 0,
    fileTotal = 3,
    fs = require('fs'),
    compileFile = function() {
        fs.readFile(includes[filesLoaded], 'utf8', function readFileResponse(err, data) {
            if(err) {
                console.log(err);
            } else {
                html += data;
                filesLoaded++;
                if(filesLoaded >= fileTotal) {
                    writeIndex();
                    return;
                }
                compileFile();
            }
        });
    },
    writeIndex = function() {
            console.log(html);
            fs.writeFile('index.html', html, function(err) {
                if(err) {
                    console.log("file not written: " + err);
                } else {
                    console.log("Compiled file in " + String(Date.now() - startTime) + "ms");
                }
            });
    }

compileFile();

1 个答案:

答案 0 :(得分:5)

看起来otherDirection的值在其末尾有一个空格,因此它们不相等,因为那里有!,整个表达式为true