简体   繁体   English

如何替换 ArrayList 的特定部分?

[英]How to replace a specific part of an ArrayList?

So I have an ArrayList here:所以我这里有一个ArrayList

story.add("Select the type of bread you want to use. Many prefer the taste of [Color] bread, while others prefer [Noun1] bread because it is healthy.");
story.add("Choose the flavor of Jam/Jelly. I personally prefer [Food] jam, but you can use whatever you want.");
story.add(" Choose the type of peanut butter - either [Adjective1] or [Adjective2].");
story.add("Take out [Number] slice(s) of bread.");
story.add(" Use a [Noun2] to [Verb1] the jam all over on of the pieces of bread.");
story.add(" Now [Verb2] the peanut butter on the other piece of bread.");
story.add("Put them together, and you have a PB&J [Verb3].");

I am trying to replace an individual part of the ArrayList .我正在尝试替换ArrayList的单个部分。 For example, I want to replace [Color] with an actual color without replacing the whole ArrayList .例如,我想用实际颜色替换[Color]而不替换整个ArrayList Is this possible?这可能吗?

Thanks.谢谢。

Use a ListIterator , so you can replace the value while iterating:使用ListIterator ,因此您可以在迭代时替换该值:

for (ListIterator<String> iter = story.listIterator(); iter.hasNext(); ) {
    String line = iter.next();
    line = line.replace("[Color]", "BLUE");
    iter.set(line);
}

story.forEach(System.out::println);

Output输出

Select the type of bread you want to use. Many prefer the taste of BLUE bread, while others prefer [Noun1] bread because it is healthy.
Choose the flavor of Jam/Jelly. I personally prefer [Food] jam, but you can use whatever you want.
 Choose the type of peanut butter - either [Adjective1] or [Adjective2].
Take out [Number] slice(s) of bread.
 Use a [Noun2] to [Verb1] the jam all over on of the pieces of bread.
 Now [Verb2] the peanut butter on the other piece of bread.
Put them together, and you have a PB&J [Verb3].

If you want to replace many of the placeholders in a single iteration, then do it like this (Java 9+):如果你想在一次迭代中替换许多占位符,那么这样做(Java 9+):

Map<String, String> map = new HashMap<>();
map.put("Color", "BLUE");
map.put("Adjective1", "SMOOTH");
map.put("Adjective2", "CHUNKY");
map.put("Number", "THREE");

Pattern p = Pattern.compile("\\[([^\\]]+)\\]");
for (ListIterator<String> iter = story.listIterator(); iter.hasNext(); ) {
    String line = iter.next();
    line = p.matcher(line).replaceAll(mr -> map.getOrDefault(mr.group(1), mr.group(0)));
    iter.set(line);
}

Output输出

Select the type of bread you want to use. Many prefer the taste of BLUE bread, while others prefer [Noun1] bread because it is healthy.
Choose the flavor of Jam/Jelly. I personally prefer [Food] jam, but you can use whatever you want.
 Choose the type of peanut butter - either SMOOTH or CHUNKY.
Take out THREE slice(s) of bread.
 Use a [Noun2] to [Verb1] the jam all over on of the pieces of bread.
 Now [Verb2] the peanut butter on the other piece of bread.
Put them together, and you have a PB&J [Verb3].

I am assuming this is an ArrayList of String 's like so:我假设这是一个String的 ArrayList ,如下所示:

ArrayList<String> story = new ArrayList<String>();
story.add("Select the type of bread you want to use. Many prefer the taste of [Color] bread, while others prefer [Noun1] bread because it is healthy.");
...

In this case, you need to replace the CharSequence [Color] with your new value:在这种情况下,您需要将 CharSequence [Color]替换为您的新值:

story.get(0).replace("[Color]", "yellow");

If you want to overwrite the original ArrayList with the replaced value you would do...如果你想用替换的值覆盖原始的 ArrayList 你会做...

story.set(0, story.get(0).replace("[Color]", "yellow"));

Well,you don't have to iterate over Array.It can also be done by using stream .好吧,您不必遍历 Array。也可以使用stream来完成。 You can change any value in the collection using one line of code with .stream() method.您可以使用带有 .stream() 方法的一行代码更改集合中的任何值。

    story.stream().map( n ->n.replace("VALUE_YOU_WANT_TO_REPLACE","VALUE_YOU_WANT_TO_BE_REPLACED")).collect(Collectors.toList());

For example if you want to replace "[Color]" with "Blue"例如,如果您想用“蓝色”替换“[颜色]”

    story = (ArrayList<String>) story.stream().
        map(n ->n.replace("[Color]","BLUE")).collect(Collectors.toList());

You can do it in one short line...你可以在一条短线中完成...

Given a map of your desired values:给定您想要的值的地图:

Map<String, String> map = Map.of("[Color]", "green", "[Food]", "fries"); // etc

This does the transform:这进行了转换:

map.forEach((k, v) -> story.replaceAll(s -> s.replace(k, v)));

See javadoc for List#replaceAll有关List#replaceAll 的信息,请参阅 javadoc

Note that this solution does not make use of regex matching.请注意,此解决方案使用正则表达式匹配的。

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

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