0

Помогите понять, почему при вызове второго it.remove() в методе test8() выбрасывает куча ошибок, а при отладке пишет Collecting data...?

Весь код прилагаю ниже + коментарии с тестом программы. Буду всем благодарен и рад услышать ваши предложения!!!

import java.util.Iterator;
import java.util.Objects;

public class ListImpl implements List {
    private Node head;
    private Node tail;
    int current = 0;

    @Override
    public void addFirst(Object element) {
        Node a = new Node();
        a.item = element;

        if (head == null) {
            head = a;
            tail = a;
        } else {
            a.next = head;
            a.prev = tail;
            head = a;
        }
        current++;
    }

    @Override
    public void addLast(Object element) {
        Node a = new Node();
        a.item = element;

        if (tail == null) {
            head = a;
            tail = a;
        } else {
            tail.next = a;
            a.prev = tail;
            tail = a;
        }
        current++;
    }

    @Override
    public void removeFirst() {
        if (head == null) {
            current = 0;
        } else if (head == tail) {
            head = null;
            tail = null;
            current = 0;
        } else {
            head = head.next;
        }
        current--;
    }

    @Override
    public void removeLast() {
        Node remLast = null;
        Node count = head;

        while (count != null) {
            if (head == null) {
                current = 0;
            } else if (head == tail) {
                head = null;
                tail = null;
                current = 0;
            } else {
                if (count.next == tail) {
                    remLast = count;
                    tail = remLast;
                } else {
                    count = count.next;
                }
            }
        }
        current--;
    }

    @Override
    public Object getFirst() {
        if (head == null) {
            return "";
        } else {
            return head.item;
        }
    }

    @Override
    public Object getLast() {
        Node count = head;

        while (count.next != null) {
            if (count == tail) {
                count = tail;
            } else {
                count = count.next;
            }
        }
        return count.item;
    }

    @Override
    public Object search(Object element) {
        return null;
    }

    @Override
    public boolean remove(Object element) {
        Node count = head;
        boolean remTrue = false;
        String str = element.toString();

        if (count != null) {

            while (count != null) {
                if (str.equals(count.item)) { // не заходит в if...
                    remTrue = true;
                    break;
                } else {
                    remTrue = false;
                    count = count.next;
                }
            }
        } else {
            remTrue = false;
        }

        return remTrue; // Всегда false, не пойму почему?
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ListImpl list = (ListImpl) o;
        return Objects.equals(head, list.head) &&
                Objects.equals(tail, list.tail);
    }

    @Override
    public int hashCode() {
        return Objects.hash(head, tail);
    }

    @Override
    public void clear() {
        head = null;
        tail = null;
        current = 0;
    }

    @Override
    public int size() {
        return current;
    }

    @Override
    public Iterator<Object> iterator() {
        return new IteratorImpl();
    }

    public class IteratorImpl implements Iterator<Object> {

        Node iteratorImpl = head;
        Node follow = iteratorImpl;
        boolean flag = false;

        @Override
        public boolean hasNext() {
            return iteratorImpl != null;
        }

        @Override
        public Object next() {
            follow = iteratorImpl;
            iteratorImpl = iteratorImpl.next;
            flag = true;
            return follow.item;
        }

        @Override
        public void remove() {
            Node prev = follow.prev;
            Node next = follow.next;

            if (prev != null)
                prev.next = follow.next;

            if (next != null)
                next.prev = follow.prev;

            if (follow == head)
                head = next;

            if (flag == false) {
                throw new IllegalStateException();
            }

            flag = false;
            current--;
        }
    }

    static class Node {
        Object item;
        Node next;
        Node prev;
    }



    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        Node current = head;

        while (current != null) {
            if (current.next == null) {
                stringBuilder.append(current.item);
            } else {
                stringBuilder.append(current.item).append(", ");
            }
            current = current.next;
        }

        return "[" + stringBuilder.toString() + "]";
    }

    public static void main(String[] args) {
        test8();
    }

    private static void test8() {

        List list = new ListImpl();
        list.addLast("A");
        list.addFirst("B");
        list.addLast("C");

        Iterator it = list.iterator();

        System.out.println(it.next());
        System.out.println(it.next());
        System.out.println(it.next());
        it.remove();
        System.out.println(list);

        it = list.iterator();

        System.out.println(it.next());
        it.remove(); // тут метод remove работает не правильно
        System.out.println(list);

        it = list.iterator();

        System.out.println(it.next());
        it.remove();
        System.out.println(list);

        /* an output must be as the following:
        *************************************
        B
        A
        C
        [B, A]
        B
        [A] // от сюда не выводит
        A
        []
        *************************************
        */
    }
}
2

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.