英文:
Difference between Arraylist.forEach() and Arraylist.replaceAll()
问题
关于 Arraylist.forEach()
和 Arraylist.replaceAll()
之间的区别,我有一个问题。我知道 Arraylist.forEach()
用于对列表中的每个元素执行操作,但似乎 Arraylist.replaceAll()
做的事情完全相同。但出于某种原因,当您决定执行 a.forEach(e -> e.concat("test");
假设 a 是一个 String 类型的 Arraylist 时,该 Arraylist 不会将 "test" 连接到每个 String 的末尾。然而,如果您执行 a.replaceAll(e -> e.concat("test");
,连接操作就会起作用。为什么会出现这种情况呢?我所说的情况在这张图片中有例子:
。
答案是 B,但是对我来说 C 似乎是最有道理的。感谢您的帮助。
英文:
I have a question about the difference between Arraylist.forEach()
and Arraylist.replaceAll()
. I know that Arraylist.forEach()
is used to perform an action for each element in the list, but it seems that Arraylist.replaceAll()
does the exact same thing. But for some reason, when you decide to do a.forEach(e -> e.concat("test");
assuming a is an Arraylist of Strings, the Arraylist does not concatenate "test" to the end of each String in the Arraylist. However, if you do a.replaceAll(e -> e.concat("test");
, the concatenation works. Why is this the case? An example of what I am talking about is in this image:
.
The answer is B, but C seems to make the most sense to me. Thanks for the help.
答案1
得分: 3
concat
返回一个新字符串,但 forEach
忽略了 lambda 表达式的返回值。(它声明的参数是一个没有返回值的 Consumer
。)
replaceAll
将其函数的返回值放回列表中。这就是它的用途。
英文:
concat
returns a new string, but forEach
ignores the return value of the lambda expression. (Its declared parameter is a Consumer
, which has no return value.)
replaceAll
puts the return value from its function back into the list. That is what it is for.
答案2
得分: 0
记住字符串是不可变的。在调用forEach()
时,你没有将concat
的返回值赋值给任何变量,但是replaceAll()
会覆盖当前元素,因为它在每次调用时都会重写当前元素。
英文:
Remember that strings are immutable. You are not assigning the return value of concat
to anything when you call forEach()
, but replaceAll()
does since it overwrites the current element each time.
答案3
得分: 0
.forEach()
遍历列表的每个单独元素,然后应用函数。通常你会调用对象的函数。在这种情况下,你会丢弃返回的值,并且不会改变对象。
.replaceAll()
遍历列表的每个单独元素并应用一个函数。之后,返回的值将被设置在特定的位置。
.concat()
由于字符串的不可变性不会改变字符串引用,但它会返回一个新的引用,其中包含更改后的字符串。
英文:
.forEach()
takes each individual element of the list and then applies the function. Normally you would call a function of an object. In this case you throw the returned value away and does not change the object.
.replaceAll()
takes each individual element of the list and applices a function. After that the returned value will be set on the specific place.
.concat()
does not change the string reference due to the string immutability, but it will return the changed string in a new reference.
答案4
得分: 0
forEach方法将每个元素传递给函数,但只要对象是不可变类型,它就不会修改数组成员。因此,它不期望函数有返回值(void)。数组始终保持不变。字符串类型是不可变的,因此任何更改都不会影响数组中保存的对象。
replaceAll方法将所有数组成员传递给函数,并检查函数返回的对象,当然会将该对象存回数组,即进行替换。需要注意的是,在此方法中,函数必须返回一个对象。例如在String.concat中,连接的字符串被返回,因此该连接后的字符串出现在数组中。
英文:
The names explain exactly what the methods do.
forEach pass each element to the function, but it doesn't modify the array member as long as the object is of an immutable type. Consequently it expects no return value (void) of the function. The array always remain unchanged. String type is immutable, so any change doesn't influence the object being held in the array.
replaceAll pass all array members to the function and checks the object return by that function, and of course store that object back to the array, meaning replace. Note that in this method the function must return an object. In case of String.concat the concatenated String is returned thus that concatenated String appears in the array.
答案5
得分: 0
我认为发生这种情况是因为:
- a.forEach遍历了ArrayList中的每个元素,然后Concat返回一个新字符串。它不会改变ArrayList中的原始字符串。
- a.Replaceall将替换ArrayList中的原始字符串。
英文:
I think it happens because
- a.forEach iterates over each element in the arrayList, then Concat returns a new string. It does not change the original string in the arrayList.
- a.Replaceall will replace the original string in the arrayList.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论