Guest User

Untitled

a guest
Dec 13th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. package a_Zadania.a_Dzien_1.a_kolekcje_1;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.ListIterator;
  6.  
  7. /*W pliku Main4.java:
  8.  
  9. Napisz metodę public static List<City> reverse (List<City> list ), która zwróci listę elementów w odwrotnej do otrzymanej kolejności.
  10. Zapoznaj się z możliwościami klasy ListIterator.*/
  11. public class Main4 {
  12.  
  13. public static void main(String[] args) {
  14.  
  15. City city1 = new City("Hamburg", 4000);
  16. City city2 = new City("Gdańsk", 44000);
  17. City city3 = new City("Myślęcinek", 99000);
  18. City city4 = new City("Kraków", 47600);
  19. City city5 = new City("Londyn", 872000);
  20.  
  21. List<City> cityList = new ArrayList<City>();
  22. cityList.add(city1);
  23. cityList.add(city2);
  24. cityList.add(city3);
  25. cityList.add(city4);
  26. cityList.add(city5);
  27.  
  28. reverse(cityList);
  29. }
  30.  
  31. public static List<City> reverse(List<City> list) {
  32.  
  33. ListIterator<City> iter = list.listIterator(list.size());
  34. List<City> temp = new ArrayList<>();
  35. System.out.println("------------");
  36. while (iter.hasPrevious()) {
  37. System.out.println(iter.previous());
  38. temp.add(iter.previous());
  39.  
  40. }
  41. return temp;
  42. }
  43. }
Add Comment
Please, Sign In to add comment