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

The*_*ook 1 java arraylist

我正在将我direcList分成两个子列表firstDirec,secondtDirecNew direction发现一个特殊的字符串时.

然后我通过不将它们添加到新的ArrayLists 来过滤列表中的特定字符串timeListFirst and timeListSecond.一切正常firstDirec,并且元素没有添加到timeListFirst,但我secondtDirec在这个位置的列表面临问题:

if (!(mergeLine.equals(otherDirection))
Run Code Online (Sandbox Code Playgroud)

因为在这个位置mergeLine等于"Ravensbusch",所以是 otherDirection.尽管如此,这个字符串正被添加到timeListSecond.有人可以解释我的错误吗?

mergeLine值:

在此输入图像描述

otherDirection值:

在此输入图像描述

        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);

                    }
                }
Run Code Online (Sandbox Code Playgroud)

Net*_*eta 5

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