你如何通过一个ArrayList迭代,在每个对象执行操作,并将其存储回ArrayList中? [英] How do you iterate through an arraylist , perform an operation in each object and store it back in the arraylist?

查看:119
本文介绍了你如何通过一个ArrayList迭代,在每个对象执行操作,并将其存储回ArrayList中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何通过一个ArrayList迭代,在每个对象执行操作,并将其存储回ArrayList中?

How do you iterate through an arraylist , perform an operation in each object and store it back in the arraylist?

这过程中频繁出现pretty,我已经找到了可行的解决方案,但我想知道解决这种情况下,最好的做法是什么。

This process comes up pretty frequently and I have found a workable solution , but I would like to know what the best practice for addressing this situation is.

我的一般做法


  • 通过ArrayList的每个对象迭代

  • Iterate through each object of the ArrayList

获取当前指向的对象的副本

Get a copy of the object currently being pointed to

上的副本进行了手术。

覆盖与修改后的副本ArrayList的原始项目

Overwrite the original item in the arraylist with the modified copy

它的工作原理,但我不知道是否有一个更有效的,或不容易出错的技术。

It works, but I wonder if there is a more efficient or less error prone technique.

感谢您。

推荐答案

通常情况下,你不需要这么做。

Usually, you don't need to do this.


  • 获取当前指向的对象的副本

  • Get a copy of the object currently being pointed to

上的副本进行了手术。

覆盖与修改后的副本ArrayList的原始项目

Overwrite the original item in the arraylist with the modified copy

您只需要获得原始对象并修改它。然后你结果
并不需要将其存储回数组列表,它已经存在。

You just need to get the original object and modify it. Then you
don't need to store it back to the array list, it is already there.

这篇关于你如何通过一个ArrayList迭代,在每个对象执行操作,并将其存储回ArrayList中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆