可以以相反的顺序为java中的每个循环做一个吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1098117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can one do a for each loop in java in reverse order?
提问by Ron Tuffin
I need to run through a List in reverse order using Java.
我需要使用 Java 以相反的顺序运行 List。
So where this does it forwards:
那么它在哪里转发:
for(String string: stringList){
//...do something
}
Is there some way to iterate the stringList in reverse order using the for eachsyntax?
有什么方法可以使用for each语法以相反的顺序迭代 stringList吗?
For clarity: I know how to iterate a list in reverse order but would like to know (for curiosity's sake ) how to do it in the for eachstyle.
为清楚起见:我知道如何以相反的顺序迭代列表,但想知道(出于好奇)如何在每种样式中执行此操作。
采纳答案by Nat
The Collections.reverse method actually returns a new list with the elements of the original list copied into it in reverse order, so this has O(n) performance with regards to the size of the original list.
Collections.reverse 方法实际上返回一个新列表,其中原始列表的元素以相反的顺序复制到其中,因此相对于原始列表的大小,它的性能为 O(n)。
As a more efficient solution, you could write a decorator that presents a reversed view of a List as an Iterable. The iterator returned by your decorator would use the ListIterator of the decorated list to walk over the elements in reverse order.
作为更有效的解决方案,您可以编写一个装饰器,将 List 的反向视图显示为 Iterable。装饰器返回的迭代器将使用装饰列表的 ListIterator 以相反的顺序遍历元素。
For example:
例如:
public class Reversed<T> implements Iterable<T> {
private final List<T> original;
public Reversed(List<T> original) {
this.original = original;
}
public Iterator<T> iterator() {
final ListIterator<T> i = original.listIterator(original.size());
return new Iterator<T>() {
public boolean hasNext() { return i.hasPrevious(); }
public T next() { return i.previous(); }
public void remove() { i.remove(); }
};
}
public static <T> Reversed<T> reversed(List<T> original) {
return new Reversed<T>(original);
}
}
And you would use it like:
你会像这样使用它:
import static Reversed.reversed;
...
List<String> someStrings = getSomeStrings();
for (String s : reversed(someStrings)) {
doSomethingWith(s);
}
回答by Uri
AFAIK there isn't a standard "reverse_iterator" sort of thing in the standard library that supports the for-each syntax which is already a syntactic sugar they brought late into the language.
AFAIK 标准库中没有标准的“reverse_iterator”类型的东西支持 for-each 语法,这已经是他们后期引入语言的语法糖。
You could do something like for(Item element: myList.clone().reverse()) and pay the associated price.
您可以执行类似 for(Item element: myList.clone().reverse()) 的操作并支付相关价格。
This also seems fairly consistent with the apparent phenomenon of not giving you convenient ways to do expensive operations - since a list, by definition, could have O(N) random access complexity (you could implement the interface with a single-link), reverse iteration could end up being O(N^2). Of course, if you have an ArrayList, you don't pay that price.
这似乎也与没有为您提供方便的方法来执行昂贵操作的明显现象相当一致 - 因为根据定义,列表可能具有 O(N) 随机访问复杂度(您可以使用单链接实现接口),反向迭代最终可能是 O(N^2)。当然,如果你有一个 ArrayList,你就不用付出那个代价。
回答by casperOne
Not without writing some custom code which will give you an enumerator which will reverse the elements for you.
并非没有编写一些自定义代码,这些代码将为您提供一个枚举器,它将为您反转元素。
You should be able to do it in Java by creating a custom implementation of Iterable which will return the elements in reverse order.
您应该能够通过创建 Iterable 的自定义实现来在 Java 中执行此操作,该实现将以相反的顺序返回元素。
Then, you would instantiate the wrapper (or call the method, what-have-you) which would return the Iterable implementation which reverses the element in the for each loop.
然后,您将实例化包装器(或调用方法,what-have-you),它将返回 Iterable 实现,该实现反转 for each 循环中的元素。
回答by Allan
You can use the Collections class http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.htmlto reverse the list then loop.
您可以使用 Collections 类http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html来反转列表然后循环。
回答by Owen
You'd need to reverse your collection if you want to use the for each syntax out of the box and go in reverse order.
如果您想开箱即用地使用 for each 语法并以相反的顺序进行,则需要反转您的集合。
回答by Jon Skeet
For a list, you could use the Google Guava Library:
对于列表,您可以使用Google Guava Library:
for (String item : Lists.reverse(stringList))
{
// ...
}
Note that Lists.reverse
doesn'treverse the whole collection, or do anything like it - it just allows iteration and random access, in the reverse order. This is more efficient than reversing the collection first.
请注意,这不会反转整个集合,也不会执行类似的操作 - 它只允许以相反的顺序进行迭代和随机访问。这比首先反转集合更有效。Lists.reverse
To reverse an arbitrary iterable, you'd have to read it all and then "replay" it backwards.
要反转任意可迭代对象,您必须阅读所有内容,然后向后“重播”它。
(If you're not already using it, I'd thoroughlyrecommend you have a look at the Guava. It's great stuff.)
(如果你还没有使用它,我会彻底建议你看看番石榴。这是伟大的东西。)
回答by Phillip Gibb
This will mess with the original list and also needs to be called outside of the loop.
Also you don't want to perform a reverse every time you loop - would that be true if one of the Iterables.reverse ideas
was applied?
这将与原始列表混淆,并且还需要在循环外调用。此外,您不想在每次循环时都执行反向操作 - 如果应用了其中之一,那会是真的Iterables.reverse ideas
吗?
Collections.reverse(stringList);
for(String string: stringList){
//...do something
}
回答by Krishna
This may be an option. Hope there is a better way to start from last element than to while loop to the end.
这可能是一种选择。希望有更好的方法从最后一个元素开始而不是 while 循环到最后。
public static void main(String[] args) {
List<String> a = new ArrayList<String>();
a.add("1");a.add("2");a.add("3");a.add("4");a.add("5");
ListIterator<String> aIter=a.listIterator();
while(aIter.hasNext()) aIter.next();
for (;aIter.hasPrevious();)
{
String aVal = aIter.previous();
System.out.println(aVal);
}
}
回答by Gopi Reddy
The List (unlike the Set) is an ordered collection and iterating over it does preserve the order by contract. I would have expected a Stack to iterate in the reverse order but unfortunately it doesn't. So the simplest solution I can think of is this:
List(与 Set 不同)是一个有序集合,迭代它确实按合同保留了顺序。我本来希望 Stack 以相反的顺序迭代,但不幸的是它没有。所以我能想到的最简单的解决方案是:
for (int i = stack.size() - 1; i >= 0; i--) {
System.out.println(stack.get(i));
}
I realize that this is not a "for each" loop solution. I'd rather use the for loop than introducing a new library like the Google Collections.
我意识到这不是“for each”循环解决方案。我宁愿使用 for 循环而不是引入像 Google Collections 这样的新库。
Collections.reverse() also does the job but it updates the list as opposed to returning a copy in reverse order.
Collections.reverse() 也可以完成这项工作,但它会更新列表,而不是以相反的顺序返回副本。
回答by serv-inc
As of the comment: You should be able to use Apache Commons ReverseListIterator
截至评论:您应该能够使用 Apache CommonsReverseListIterator
Iterable<String> reverse
= new IteratorIterable(new ReverseListIterator(stringList));
for(String string: reverse ){
//...do something
}
As @rogerdpack said, you need to wrap the ReverseListIterator
as an Iterable
.
作为@rogerdpack说,你需要用的ReverseListIterator
作为Iterable
。