【发布时间】:2020-11-22 03:38:59
【问题描述】:
public class modifyALelements {
public static void main(String[] args) {
StringBuilder myArr [] = {new StringBuilder("One"), new StringBuilder("Two")};
for (StringBuilder val: myArr)
System.out.println(val);
for (StringBuilder val: myArr)
val.append("Way");
for (StringBuilder val: myArr)
System.out.println(val);
StringBuilder anotherArr[] = { new StringBuilder("Java"), new StringBuilder("Loop") };
for (StringBuilder element : anotherArr)
System.out.println(element);
for (StringBuilder element : anotherArr)
element = new StringBuilder("Test");
for (StringBuilder element : anotherArr)
System.out.println(element);
}
}
输出:
One
Two
OneWay
TwoWay
Java
Loop
Java
Loop
大家好, 有人可以解释为什么 anotherArr 的值没有更改为 Test last two for-each 循环。当您使用 append() 方法时,将应用更改,例如单向和双向。 非常感谢。
【问题讨论】:
标签: java string loops arraylist builder