List的迭代使用 modyfing 字符串

发布于 2024-10-15 03:16:09 字数 147 浏览 5 评论 0原文

我无法以这种方式修改列表的元素:

for (String s : list)
{
   s = "x" + s;
}

执行此代码后,该列表的元素不会更改 如何用最简单的方式通过List实现modyfing的迭代。

I can't modyfing element of List this way:

for (String s : list)
{
   s = "x" + s;
}

After execution this code elements of this list are unchanged
How to achieve iteration with modyfing through List in the simplest way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

荒路情人 2024-10-22 03:16:09

由于 String 对象是不可变的,因此您无法更改正在迭代的值。此外,您无法修改在这样的循环中迭代的列表。执行此操作的唯一方法是使用标准循环迭代列表索引或使用 ListIterator 接口:

for (int i = 0; i < list.size(); i++)
{
    list.set(i, "x" + list.get(i));
}

for (ListIterator i = list.listIterator(); i.hasNext(); )
{
    i.set("x" + i.next());
}

Since String objects are immutable, you cannot change the values you're iterating over. Furthermore, you cannot modify the list you're iterating over in such a loop. The only way to do this is to iterate over the list indexes with a standard loop or to use the ListIterator interface:

for (int i = 0; i < list.size(); i++)
{
    list.set(i, "x" + list.get(i));
}

for (ListIterator i = list.listIterator(); i.hasNext(); )
{
    i.set("x" + i.next());
}
傾旎 2024-10-22 03:16:09

字符串是不可变的野兽,因此我建议遵循这一理念并创建新列表而不是修改列表:

List<String> mappedList = new ArrayList<String>();

for (String s : list) {
    mappedList.add("x" + s);
}

我相信,这将使您的代码更易于理解和维护。

Strings are immutable beasts, so I can recommend to follow this philosophy and create new list instead modifying one:

List<String> mappedList = new ArrayList<String>();

for (String s : list) {
    mappedList.add("x" + s);
}

I believe, that this will make your code easier to understand and maintain.

七月上 2024-10-22 03:16:09

像这样的事情应该可以完成这项工作:

public static void main(String[] args) throws Exception {
    final List<String> lst = Arrays.asList("a", "b", "c");
    for(final ListIterator<String> iter = lst.listIterator(); iter.hasNext(); ) {
        final String s = iter.next();
        iter.set(s + "x");
    }
    System.out.println(lst);
}

Something like this should do the job:

public static void main(String[] args) throws Exception {
    final List<String> lst = Arrays.asList("a", "b", "c");
    for(final ListIterator<String> iter = lst.listIterator(); iter.hasNext(); ) {
        final String s = iter.next();
        iter.set(s + "x");
    }
    System.out.println(lst);
}
↙温凉少女 2024-10-22 03:16:09

Java 字符串是不可变的,因此无法修改。此外,如果您希望修改列表,请使用迭代器接口。

Java strings are immutable, hence they cannot be modified. Further, if you wish to modify a list use the iterator interface.

薯片软お妹 2024-10-22 03:16:09

正如其他人指出的那样:

  • 您无法修改 Java 中的字符串,因此 s = "x" + s 将创建一个新字符串(不会包含在列表中)
  • 即使您可以,变量 s 是局部变量,分配给它时,不会影响列表中包含的值。

在这种情况下,解决方案是使用 StringBuilder 来表示您可以实际修改的字符串,或者使用 ListIterator 作为 @Michael Borgwardt @jarnbjo 指出。


使用 StringBuilder

List<StringBuilder> someStrings = new LinkedList<StringBuilder>();
someStrings.add(new StringBuilder("hello"));
someStrings.add(new StringBuilder("world"));

for (StringBuilder s : someStrings)
    s.insert(0, "x");

使用 ListIterator

List<String> someStrings = new LinkedList<String>();
someStrings.add("hello");
someStrings.add("world");

for (ListIterator<String> iter = someStrings.listIterator(); iter.hasNext();)
    iter.set("x" + iter.next());

ideone.com 演示

As others have pointed out:

  • You can't modify strings in Java, so s = "x" + s will create a new string (which will not be contained in the list)
  • Even if you could, the variable s is a local variables, which, when assigned to, does not affect the values contained in the list.

The solution is in this case to use a StringBuilder which represents a string which you can actually modify, or to use a ListIterator as @Michael Borgwardt and @jarnbjo points out.


Using a StringBuilder:

List<StringBuilder> someStrings = new LinkedList<StringBuilder>();
someStrings.add(new StringBuilder("hello"));
someStrings.add(new StringBuilder("world"));

for (StringBuilder s : someStrings)
    s.insert(0, "x");

Using a ListIterator:

List<String> someStrings = new LinkedList<String>();
someStrings.add("hello");
someStrings.add("world");

for (ListIterator<String> iter = someStrings.listIterator(); iter.hasNext();)
    iter.set("x" + iter.next());

ideone.com demo

ㄖ落Θ余辉 2024-10-22 03:16:09

在循环中,您只是修改字符串的本地副本。更好的选择是使用列表的迭代器,并替换列表的当前位置。

编辑,哎呀,速度太慢了。

In your loop you're just modifying the local copy of the String. A better alternative would be to use the iterator of the list, and replace the current position of the list.

Edit, Oops, way to slow.

路弥 2024-10-22 03:16:09

您无法以这种方式修改 ListString 元素,但 StringBuilder 可以正常工作:

for (StringBuilder sb : list) sb.append("x");

对于其他基元也是如此对比参考情况和 for-each 循环。在循环中,Iterable 是不可变的,但其中项的状态不是 - 基元(如 String)没有状态,因此您只需修改一个本地副本,但引用可以有状态,因此您可以通过它们可能具有的任何变异方法(例如,sb.append("x"))来改变它们。

You can't modify a String element of a List that way, but a StringBuilder would work just fine:

for (StringBuilder sb : list) sb.append("x");

The same is true for other primitive vs reference situations and the for-each loop. In the loop, the Iterable is immutable, but the state of items in it is not - primitives (like String) do not have state and hence you're only modifying a local copy, but references can have state and hence you can mutate them via any mutator methods they might have (e.g., sb.append("x")).

天煞孤星 2024-10-22 03:16:09

另一种选择是使用 replaceAll()

List<String> stringList = new ArrayList<>(List.of(myStrings));
serialsList.replaceAll(s -> "x" + s);

Another option, use replaceAll()

List<String> stringList = new ArrayList<>(List.of(myStrings));
serialsList.replaceAll(s -> "x" + s);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文