0

So I have an ArrayList here:

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. For example, I want to replace [Color] with an actual color without replacing the whole ArrayList. Is this possible?

Thanks.

2

4 Answers 4

2

Use a ListIterator, so you can replace the value while iterating:

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+):

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].
Sign up to request clarification or add additional context in comments.

Comments

0

I am assuming this is an ArrayList of String's like so:

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:

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

If you want to overwrite the original ArrayList with the replaced value you would do...

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

2 Comments

Do you know what unreachable statement means?
@TheGamer27 An unreachable statement is a line of code that will never be run. For example, if I have a method and "return True" as the first line of my method, anything after this line will be unreachable (because the method stops executing when we hit the return line)
0

Well,you don't have to iterate over Array.It can also be done by using stream. You can change any value in the collection using one line of code with .stream() method.

    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());

2 Comments

Actually, using a stream doesn't avoid iterating over the array, it just hides the iteration a little bit.
True that; it's just important to remember that even though one no longer sees the traditional direct evidence (i.e., a for ordo loop) that an iteration is happening, an iterative process is indeed taking place.
0

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

Note that this solution does not make use of regex matching.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.