解决方案
以下示例演示了如何借助 Collection 和 Listiterator 类的 listIterator() 和 Collection.reverse() 方法来反转集合。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
public class UtilDemo3 {
public static void main(String[] args) {
String[] coins = { "A", "B", "C", "D", "E" };
List l = new ArrayList();
for (int i = 0; i < coins.length; i++)l.add(coins[i]);
ListIterator liter = l.listIterator();
System.out.println("Before reversal");
while (liter.hasNext())System.out.println(liter.next());
Collections.reverse(l);
liter = l.listIterator();
System.out.println("After reversal");
while (liter.hasNext())System.out.println(liter.next());
}
}