以下實例示範如何使用Collection 和Listiterator 類別的listIterator() 和collection.reverse() 方法來反轉集合中的元素:
/* author by w3cschool.cc Main.java */ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.ListIterator; class Main { 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("反转前"); while (liter.hasNext()) System.out.println(liter.next()); Collections.reverse(l); liter = l.listIterator(); System.out.println("反转后"); while (liter.hasNext()) System.out.println(liter.next()); } }
以上就是Java 實例- 集合反轉的內容,更多相關內容請關注PHP中文網(www.php.cn)!