简体   繁体   中英

Pattern iterator

I have a task, which consists of pattern implementation (Iterator) and addition of the method iterator() in previously created classes Hotel and Tours. Unfortunately, I'm still new to Java, so I don't know how to use methods from IIterator for arraylist inter. Info - interface

public class IIterator<T> implements Iterator<T> {

    private Object elements;
    int index = 0;


    public boolean hasNext() {
        return index< Array.getLength(elements);
    }
    public T next() {
        return (T) Array.get(elements, index++);
    }
    public void remove() {
        throw new UnsupportedOperationException("remove");
    }
}
public class Hotel implements Info, Serializable, Cloneable, Comparable{
    public Iterator iterator() {
        return new IIterator();
    }
}
public class Tours implements Info, Serializable, Cloneable, Comparable{
    public Iterator iterator() {
        return new IIterator();
    }
}

ArrayList<Info> inter = new ArrayList();
        inter.add(new Hotel("Lara", 100, 5));
        inter.add(new Hotel("Katya", 10, 1));
        inter.add(new Tours("Lara", 1000, length));
        inter.add(new Tours("HelsinkiPanorama", 1010, length));

EDITED

you can do this:

    for (Info info : inter) {
        System.out.println(info.getString());
    }

or

    Iterator<Info> it = inter.iterator();
    while (it.hasNext()){
        System.out.println(it.next().getString());
    }

Previous answer

I'm not sure about your question, but if you want a simple example of iterator you can see this:

public class Hotel {

    private String name;

    public Hotel(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public class Tours implements Iterable<Hotel>{

    private List<Hotel> hotels = new ArrayList<>();

    public void addHotel(Hotel  hotel){
        hotels.add(hotel);
    }

    @Override
    public Iterator<Hotel> iterator() {
        return hotels.iterator();
    }        
}

Example:

public static void main(String[] args) {
    Tours tours = new Tours();
    tours.addHotel(new Hotel("hotel-0"));
    tours.addHotel(new Hotel("hotel-1"));
    tours.addHotel(new Hotel("hotel-2"));
    tours.addHotel(new Hotel("hotel-3"));

    for (Hotel hotel : tours) {
        System.out.println(hotel.getName());
    }
}

It prints:

hotel-0
hotel-1
hotel-2
hotel-3

If you want to see an example under the hood

public class Tours implements Iterable<Hotel>{

    private int numHotels = 0;
    private Hotel[] hotels = null;

    public void addHotel(Hotel  hotel){
        if (hotels == null){
            hotels = new Hotel[5];
        } else if ( numHotels + 1 >= hotels.length){
            hotels = Arrays.copyOf(hotels, numHotels + 5);
        }
        hotels[numHotels++] = hotel;
    }

    @Override
    public Iterator<Hotel> iterator() {
        return new HotelIterator();
    }

    private class HotelIterator implements Iterator<Hotel> {
        private int index = 0;

        @Override
        public boolean hasNext() {
            return index < numHotels;
        }

        @Override
        public Hotel next() {
            return hotels[index++];
        }
    }
}

Example 2:

public static void main(String[] args) {
    Tours tours = new Tours();
    tours.addHotel(new Hotel("hotel-0"));
    tours.addHotel(new Hotel("hotel-1"));
    tours.addHotel(new Hotel("hotel-2"));
    tours.addHotel(new Hotel("hotel-3"));

    for (Hotel hotel : tours) {
        System.out.println(hotel.getName());
    }
}

It prints:

hotel-0
hotel-1
hotel-2
hotel-3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM