Enhanced for loop and Array 增强型for循环和数组

huangapple go评论44阅读模式
英文:

Enhanced for loop and Array

问题

以下是代码的翻译部分:

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)中的值在最后两个for-each循环中没有更改为"Test",这是因为在这些循环中,你只是修改了元素的引用,而没有修改元素本身。要修改元素的内容,你需要使用append()等方法,就像你在第一个数组(myArr)中所做的那样。

英文:
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);
	
	}
}

output:

One
Two
OneWay
TwoWay
Java
Loop
Java
Loop

Hi all,
Can someone please explain why has values of anotherArr not changed to Test last two for-each loops. When you use append() method, changes are applied e.g. OneWay and TwoWay.
Many thanks.

答案1

得分: 1

在Java中,类类型是引用类型。这意味着类型为 StringBuilder 的变量将保存对 StringBuilder 对象的引用

for (StringBuilder val : myArr)
    val.append("Way");

在这里,我们从 myArr 中获取引用。然后我们调用 append 方法,它会修改所引用的对象。

for (StringBuilder element : anotherArr)
    element = new StringBuilder("Test");

在这里,我们同样从数组中获取引用。然而,然后我们重新分配了一个变量。这仅仅改变了 element 变量引用的对象,实际上没有改变任何对象。

英文:

In java, class types are reference types. This means that a variable of type StringBuilder will hold a reference to a StringBuilder object.

for (StringBuilder val: myArr)
    val.append("Way");

Here we take references from myArr. Then we call method append, which changes the referred object.

for (StringBuilder element : anotherArr)
    element = new StringBuilder("Test");

Here we also take a reference from an array. However, then we reassign a variable. This only changes what element variable refers to. No object is actually changed.

huangapple
  • 本文由 发表于 2020年8月1日 22:22:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63206261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定