在java中修改List的每一项 [英] Modifying each item of a List in java

查看:60
本文介绍了在java中修改List的每一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Java 中的列表.我想知道修改列表中每个元素的推荐方法是什么?

I'm just starting to work with lists in java. I'm wondering what the recommended method to modify each element of a list would be?

我已经能够使用以下两种方法完成它,但它们看起来都相当不优雅.有没有更好的方法在java中完成这项工作?是否推荐以下任何一种方法,或者两者处于同一级别?

I've been able to get it done with both the following methods, but they both seem fairly unelegant. Is there any better way to get this done in java? And is any of the below methods recommended over the other, or are both on the same level?

//Modifying with foreach
for (String each : list)
{
    list.set(list.indexOf(each), each+ " blah");
}

//Modifying with for
for (ListIterator<String> i = list.listIterator(); i.hasNext(); i.next()) 
{
    i.next();
    list.set(i.nextIndex()-1, i.previous() + " blah yadda");
}

推荐答案

第二个版本会更好.在内部它们最终是相同的,但第二个实际上允许您修改列表,而第一个将抛出 ConcurrentModificationException.

The second version would be better. Internally they are the same in the end, but the second actually allows you to modify the list, while the first one will throw a ConcurrentModificationException.

但是您以错误的方式使用了迭代器.以下是您如何正确执行此操作:

But then you are using the Iterator in a wrong way. Here is how you do it correctly:

for (final ListIterator<String> i = list.listIterator(); i.hasNext();) {
  final String element = i.next();
  i.set(element + "yaddayadda");
}

迭代器是需要修改列表的迭代器,因为它是唯一知道如何正确地做到这一点而不会对列表元素和顺序感到困惑的迭代器.

The iterator is the one that needs to modify the list as it is the only one that knows how to do that properly without getting confused about the list elements and order.

因为我在所有评论和其他答案中都看到了这一点:

Because I see this in all comments and the other answers:

为什么不应该在循环中使用 list.get、list.set 和 list.size

Java 集合框架中有许多集合,每个集合都针对特定需求进行了优化.许多人使用 ArrayList,它内部使用一个数组.只要元素的数量不会随时间发生太大变化,并且具有特殊的好处,即 get、set 和 size 是对这种特定类型的列表进行恒定时间操作.

There are many collections in the Java collections framework, each on optimized for specific needs. Many people use the ArrayList, which internally uses an array. This is fine as long as the amount of elements does not change much over time and has the special benefit that get, set and size are constant time operations on this specific type of list.

然而,还有其他列表类型,但事实并非如此.例如,如果您有一个不断增长和/或缩小的列表,那么使用 LinkedList 会好得多,因为与 ArrayList 相比,add(element) 是一个恒定时间操作,但 add(index, element), get(index) 和 remove(index) 不是!

There are however other list types, where this is not true. For example if you have a list that constantly grows and/or shrinks, it is much better to use a LinkedList, because in contrast to the ArrayList add(element) is a constant time operation, but add(index, element), get(index) and remove(index) are not!

要获取特定索引的位置,需要从第一个/最后一个遍历列表,直到找到特定元素.因此,如果您在循环中执行此操作,则这等于以下伪代码:

To get the position of the specific index, the list needs to be traversed from the first/last till the specific element is found. So if you do that in a loop, this is equal to the following pseudo-code:

for (int index = 0; index < list.size(); ++index) {
  Element e = get( (for(int i = 0; i < size; ++i) { if (i == index) return element; else element = nextElement(); }) );
}

Iterator 是一种遍历列表的抽象方法,因此它可以确保以最佳方式对每个列表进行遍历.测试表明,ArrayList 使用迭代器和 get(i) 的时间差很小,但 LinkedList 上的时间差很大(有利于迭代器).

The Iterator is an abstract way to traverse a list and therefore it can ensure that the traversal is done in an optimal way for each list. Test show that there is little time difference between using an iterator and get(i) for an ArrayList, but a huge time difference (in favor for the iterator) on a LinkedList.

这篇关于在java中修改List的每一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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