Java:如何反转Iterable

时间:2016-05-09 16:17:48

标签: java eclipse iterator

我必须撤销链接列表。只要看看方法reverse,也许你可以帮助我,因为我很绝望;)。也许这对以下方法是可行的......

谢谢大家。

继承守则:

import java.util.Iterator;

public class LL<E> implements Iterable<E> {
    E head;
    LL<E> tail;
    String a="[";

    public LL(E head, LL<E> tail) {
        super();
        this.head = head;
        this.tail = tail;
    }

    public LL() {
        this(null, null);
    }

    boolean isEmpty() {
        return head == null && tail == null;
    }

    @Override
    public Iterator<E> iterator() {
        return new MyIterator(this);
    }

    private class MyIterator implements Iterator<E> {
        LL<E> current;

        public MyIterator(LL<E> current) {
            super();
            this.current = current;
        }

        @Override
        public boolean hasNext() {
            return !current.isEmpty();
        }

        @Override
        public E next() {
            E result = current.head;
            current = current.tail;
            return result;
        }
        E get(int i) throws IndexOutOfBoundsException{//maybe...
            E result = current.head;
            for(int q=0; q<i;q++){
                current=current.tail;
            }
            return result;
        }

        boolean contains(E e){//das gleiche nur mit tail
            boolean yo=false;
            while(hasNext()){
                E result= current.head;// oder tail
                if(result.equals(e))return yo=true;//unnötig ;)
            }
            return yo;
        }

        E last(){//oder nochmal
            E result=get(1);
            if(hasNext()) {result= next();};
            return result;
        }




    }

    public LL<E> limit(int i){
        if(i>0&& !isEmpty()){
            return new LL<E>(head,tail.limit(i-1));
        }
        return new LL<E>();
    }

    public LL<E> drop(int i){
        //löschen wie bei limit
        if(i>0){
            return tail.drop(i-1);
        }
        return new LL<E>(head,tail);
    }

    public LL<E> sublist(int from, int to){
        //was rauslöschen und limit
        LL<E> ersteElemente= this.limit(to);
        //erste delete
        return ersteElemente.drop(from);
    }

    public LL<E> reverse(){
        //2 schleifen
        for(int q=6;q>0;q--){
            return this.sublist(q-1, q);
        }

    }


    @Override
    public String toString(){
        String result="[";
        LL<E> n;//for Schleife

        //while (tail!=null){
            this.forEach((x)-> a+=x+";");
            a+="]";
        //}
        return a;

    }















    public static void main(String[] args) {
        LL<String> xs = new LL<>("Freunde", new LL<>("Römer", new LL<>("Landsleute",
                new LL<>("leiht", new LL<>("mir", new LL<>("euer", new LL<>("Ohr", new LL<>())))))));
        xs.forEach((x) -> System.out.println(x.toUpperCase()));
        for (String x:xs){
            System.out.println(x.toUpperCase());
        }
        System.out.println((xs.toString()));
        xs.toString();
        System.out.println("END");

    }
}

1 个答案:

答案 0 :(得分:0)

您需要以反向方法

返回一个值
    public LL<E> reverse() {
    LL<E> result = null;
    // 2 schleifen
    for (int q = 6; q > 0; q--) {
        result = this.sublist(q - 1, q);
    }
    return result;
}