Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a_Zadania.a_Dzien_1.a_kolekcje_1;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.ListIterator;
- /*W pliku Main4.java:
- Napisz metodę public static List<City> reverse (List<City> list ), która zwróci listę elementów w odwrotnej do otrzymanej kolejności.
- Zapoznaj się z możliwościami klasy ListIterator.*/
- public class Main4 {
- public static void main(String[] args) {
- City city1 = new City("Hamburg", 4000);
- City city2 = new City("Gdańsk", 44000);
- City city3 = new City("Myślęcinek", 99000);
- City city4 = new City("Kraków", 47600);
- City city5 = new City("Londyn", 872000);
- List<City> cityList = new ArrayList<City>();
- cityList.add(city1);
- cityList.add(city2);
- cityList.add(city3);
- cityList.add(city4);
- cityList.add(city5);
- reverse(cityList);
- }
- public static List<City> reverse(List<City> list) {
- ListIterator<City> iter = list.listIterator(list.size());
- List<City> temp = new ArrayList<>();
- System.out.println("------------");
- while (iter.hasPrevious()) {
- System.out.println(iter.previous());
- temp.add(iter.previous());
- }
- return temp;
- }
- }
Add Comment
Please, Sign In to add comment