[英]How to replace a specific part of an ArrayList?
所以我这里有一个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].");
我正在尝试替换ArrayList
的单个部分。 例如,我想用实际颜色替换[Color]
而不替换整个ArrayList
。 这可能吗?
谢谢。
使用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);
输出
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].
如果你想在一次迭代中替换许多占位符,那么这样做(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);
}
输出
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].
我假设这是一个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.");
...
在这种情况下,您需要将 CharSequence [Color]
替换为您的新值:
story.get(0).replace("[Color]", "yellow");
如果你想用替换的值覆盖原始的 ArrayList 你会做...
story.set(0, story.get(0).replace("[Color]", "yellow"));
好吧,您不必遍历 Array。也可以使用stream来完成。 您可以使用带有 .stream() 方法的一行代码更改集合中的任何值。
story.stream().map( n ->n.replace("VALUE_YOU_WANT_TO_REPLACE","VALUE_YOU_WANT_TO_BE_REPLACED")).collect(Collectors.toList());
例如,如果您想用“蓝色”替换“[颜色]”
story = (ArrayList<String>) story.stream().
map(n ->n.replace("[Color]","BLUE")).collect(Collectors.toList());
你可以在一条短线中完成...
给定您想要的值的地图:
Map<String, String> map = Map.of("[Color]", "green", "[Food]", "fries"); // etc
这进行了转换:
map.forEach((k, v) -> story.replaceAll(s -> s.replace(k, v)));
有关List#replaceAll 的信息,请参阅 javadoc
请注意,此解决方案不使用正则表达式匹配的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.