I found this thread on the topic and I wondered why it's so hard in Java to replace a value in a List.
My code looks ugly like this:
List<String> stringList = new ArrayList<>();
// ... add some elements
for (int i = 0; i< stringList.size(); ++i) {
if (stringList.get(i).contains("%")) {
stringList.set(i, stringList.get(i).replace("%", backupStorePath));
}
}
Is this really the only way to do this? Why can't I use a foreach loop?
for (String command : stringList) {
command = command.replace("%", backupStorePath);
}
This must be a java's "Copy by value" problem and String being immutable, but why was it implemented like that? Why is command
a copy of the original reference and not the original itself?