简体   繁体   English

模式迭代器

[英]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. 我有一个任务,包括模式实现(Iterator)和在先前创建的Hotel and Tours类中添加方法iterator()。 Unfortunately, I'm still new to Java, so I don't know how to use methods from IIterator for arraylist inter. 不幸的是,我还是Java的新手,所以我不知道如何使用IIterator中的方法进行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 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: 范例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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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