foreach 和 map 有区别吗? [英] Is there a difference between foreach and map?
问题描述
好吧,这更像是一个计算机科学问题,而不是基于特定语言的问题,但是 map 操作和 foreach 操作之间有区别吗?或者它们只是同一事物的不同名称?
Ok this is more of a computer science question, than a question based on a particular language, but is there a difference between a map operation and a foreach operation? Or are they simply different names for the same thing?
推荐答案
不同.
foreach 遍历一个列表并执行一些对每个列表成员有副作用的操作(例如将每个成员保存到数据库中)
foreach iterates over a list and performs some operation with side effects to each list member (such as saving each one to the database for example)
map 遍历一个列表,转换该列表的每个成员,并返回另一个与转换成员相同大小的列表(例如将字符串列表转换为大写)
map iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (such as converting a list of strings to uppercase)
这篇关于foreach 和 map 有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!