检查字符串是否包含字母

huangapple go评论56阅读模式
英文:

Checking if a String contains a letter

问题

我想检查我的ArrayList中的所有字符串是否包含字母"L"。

如果是的话 - 那么该字符串应该被移除。

我想知道为什么并不是每个包含"L"的字符串都被移除了?

结果是:

Rose
Leier
Rose
Rose
Rose

代码:

list.add("Leier");
list.add("Rose");
list.add("Liebe");
list.add("Leier");
list.add("Leier");
list.add("Rose");
list.add("Leier");
list.add("Rose");
list.add("Leier");
list.add("Rose");
list.add("Leier");

for (int i = 0; i < list.size(); i++) {
	if (list.get(i).matches(".*[L].*")) {
			list.remove(i);
	}
}
英文:

I wanted to check if all the Strings from my ArrayList contain a letter "L".

If yes - than the string should have been removed.

I wonder why not every string with "L" was removed?

The result is:

Rose
Leier
Rose
Rose
Rose

Code:

list.add(&quot;Leier&quot;);
list.add(&quot;Rose&quot;);
list.add(&quot;Liebe&quot;);
list.add(&quot;Leier&quot;);
list.add(&quot;Leier&quot;);
list.add(&quot;Rose&quot;);
list.add(&quot;Leier&quot;);
list.add(&quot;Rose&quot;);
list.add(&quot;Leier&quot;);
list.add(&quot;Rose&quot;);
list.add(&quot;Leier&quot;);

for (int i = 0; i &lt; list.size(); i++) {
	if (list.get(i).matches(&quot;.*[L].*&quot;)) {
			list.remove(i);
	}
}

答案1

得分: 1

从列表中删除一个项目会使所有剩余的项目向列表的前面移动。假设你移除了索引为 5 处的项目:现在索引为 6 处的项目移到了位置 5,索引为 7 处的项目移到了位置 6,依此类推。

在删除项目后,你会增加 i,所以下一步会检查索引 6 处的项目。但是索引 6 包含了曾经在索引 7 处的项目 - 你已经跳过了一个项目!

有几种方法可以解决这个问题。一种方法是使用更高级的 removeIf 方法。

list.removeIf(item -> item.matches(".*[L].*"));

另一种方法是使用 Iterator 遍历列表,并删除项目。

for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
    String item = iterator.next();
    if (item.matches(".*[L].*")) {
        iterator.remove();
    }
}

第三种方法是反转迭代的顺序:如果你从列表的末尾开始迭代,对列表索引的更改不会影响你。

for (int i = list.size() - 1; i >= 0; i--) {
    if (list.get(i).matches(".*[L].*")) {
        list.remove(i);
    }
}

顺便说一下,如果你只需要检查一个字符串是否包含字母 L,使用 contains 方法比使用 matches 更容易:item.contains("L")

英文:

When you remove an item from a list, it shifts all the remaining items towards the front of the list. So let's say you remove the item at index 5: now the item at index 6 shifts to the position of 5, and 7 to 6, and so on.

After removing the item you increment i, so next you examine index 6. But index 6 contains the item that used to be in index 7 - you've skipped over an item!

There are several ways to fix this. One is using the higher level removeIf method.

    list.removeIf(item -&gt; item.matches(&quot;.*[L].*&quot;));

Another is using an Iterator to iterate over the list, and remove items.

    for (Iterator&lt;String&gt; iterator = list.iterator(); iterator.hasNext(); ) {
        String item = iterator.next();
        if (item.matches(&quot;.*[L].*&quot;)) {
            iterator.remove();
        }
    }

A third is reversing the order of iteration: if you iterate starting from the back of the list, changes to the list indices don't affect you.

    for (int i = list.size() - 1; i &gt;= 0; i--) {
        if (list.get(i).matches(&quot;.*[L].*&quot;)) {
            list.remove(i);
        }
    }

By the way, if you really only need to check if a string contains the letter L, it's easier to use the contains method instead of matches: item.contains(&quot;L&quot;)

答案2

得分: -1

尝试一下,不要忘记将它转换为小写:

for (int i = 0; i < list.size(); i++) {
    if (list.get(i).toString().toLowerCase().contains("l")) {
        list.remove(i);
    }
}
英文:

Try this, don't forget to make it lower case

for (int i = 0; i &lt; list.size(); i++) {
            if (list.get(i).toString().toLowerCase().contains(&quot;l&quot;)) {

                list.remove(i);
            }
        }

huangapple
  • 本文由 发表于 2020年7月27日 00:09:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63102622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定