在Java中修改列表的每个项目 [英] Modifying each item of a List in java

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

问题描述

我刚刚开始使用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.

但是随后您以错误的方式使用了Iterator.这是正确执行操作的方法:

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,它内部使用数组.只要元素的数量不会随时间变化很多,这就可以了,并且具有特殊的好处,即获取,设置和大小都是在这种特定类型的列表上恒定时间操作.

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(索引)和删除(索引)不是!

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中修改列表的每个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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