[英]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.