Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t访问我的链表方法,并且可以';不要重复它_Java_Algorithm_Data Structures - Fatal编程技术网

Java Can';t访问我的链表方法,并且可以';不要重复它

Java Can';t访问我的链表方法,并且可以';不要重复它,java,algorithm,data-structures,Java,Algorithm,Data Structures,因此,我跟随这个关于数据结构的播放列表,在这个视频中总结链表部分,教授解释说我们需要一个名为IteratorHelper的内部类 视频: 这是my github中的代码,其中包含链表实现和名为tester的主类: 问题是tester类无法编译。如果将链表实例化为ListIterator,则无法访问其方法。我也不能通过它进行迭代,不管是否有迭代器助手内部类。 在视频中,他写道“implements ListI”只是ListIterator的一个较短版本? 对不起,我只是个初学者 package

因此,我跟随这个关于数据结构的播放列表,在这个视频中总结链表部分,教授解释说我们需要一个名为IteratorHelper的内部类

视频:

这是my github中的代码,其中包含链表实现和名为tester的主类:

问题是tester类无法编译。如果将链表实例化为ListIterator,则无法访问其方法。我也不能通过它进行迭代,不管是否有迭代器助手内部类。 在视频中,他写道“implements ListI”只是ListIterator的一个较短版本? 对不起,我只是个初学者

package com.ghevi.ads.linkedlists;

import java.util.ListIterator;

public class Tester {

    public static void main(String[] args) {

        ListIterator<Integer> list = new LinkedList<Integer>();
        int n = 10;

        for (int i = 0; i < n; i++)
            list.addFirstWithTail(i);


        int removedFirst = list.removeFirst();
        int removedLast = list.removeLast();

        for(int x : list){
            System.out.println(x);
        }
    }
}
package com.ghevi.ads.LinkedList;
导入java.util.ListIterator;
公共类测试员{
公共静态void main(字符串[]args){
ListIterator列表=新建LinkedList();
int n=10;
对于(int i=0;i
视频不是很清晰,但基本上
LinkedList
应该实现
Iterable
,而不是
ListIterator
IteratorHelper
应该实现
ListIterator
(参见4:20时间戳)

以下是固定代码:

package linkedlists;

import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;

// Notes at Notes/Singly LinkedList.txt

public class LinkedList<E> implements Iterable<E> {

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

    class IteratorHelper implements ListIterator<E>{

        Node<E> index;

        public IteratorHelper(){
            index = head;
        }

        // Return true if there is an element to return at the pointer
        @Override
        public boolean hasNext() {
            return (index != null);
        }

        // Return the element where the pointer is and mover the pointer to the next element
        @Override
        public E next() {
            if(!hasNext())
                throw new NoSuchElementException();

            E val = index.data;
            index = index.next;

            return val;
        }

        @Override
        public boolean hasPrevious() {
            return false;
        }

        @Override
        public E previous() {
            return null;
        }

        @Override
        public int nextIndex() {
            return 0;
        }

        @Override
        public int previousIndex() {
            return 0;
        }

        @Override
        public void remove() {

        }

        @Override
        public void set(E e) {

        }

        @Override
        public void add(E e) {

        }

        /* For version older than java 1.8
        public void remove(){
            throw new UnsupportedOperationException();
        }

        public void forEachRemaining(){};
        */

    } // inner class (can only be accessed by the outer class)

    class Node<E> {

        E data;
        Node<E> next;

        public Node(E obj){
            data = obj;
            next = null;
        }
    } // inner class (can only be accessed by the outer class)

    private Node<E> head;
    private Node<E> tail;
    private int currentSize;

    public LinkedList(){
        head = null;
        tail = null;
        currentSize = 0;
    }

    public void addFirst(E obj){
        Node<E> node = new Node<E>(obj);

        // The order of these 2 lines is fundamental
        node.next = head;
        head = node;

        currentSize++;
    }

    public void addFirstWithTail(E obj){
        Node<E> node = new Node<E>(obj);

        if(head == null){
            head = tail = node;
            return;
        }

        // The order of these 2 lines is fundamental
        node.next = head;
        head = node;

        currentSize++;
    }

    // O(n)
    public void slowAddLast(E obj){
        Node<E> node = new Node<E>(obj);

        if(head == null){
            head = tail = node;
            currentSize++;
            return;
        }

        Node<E> tmp = head;

        while(tmp.next != null){
            tmp = tmp.next;
        }

        tmp.next = node;
        currentSize++;
    }

    // O(1)
    public void fasterAddLast(E obj){
        Node<E> node = new Node<E>(obj);

        if(head == null){
            head = tail = node;
            currentSize++;
            return;
        }

        tail.next = node;
        tail = node;

        currentSize++;
    }

    public E removeFirst(){

        if(head == null){
            return null;
        }

        E tmp = head.data;

        if(head == tail){
            head = tail = null;
        } else {
            head = head.next;
        }

        currentSize--;
        return tmp;
    }

    public E removeLast(){
        if(head == null){
            return null;
        }

        if(head == tail){
            return removeFirst();
        }

        Node<E> current = head; // Can also write Node<E> current = head, previous = null;
        Node<E> previous = null;

        while(current != tail){
            // The order is crucial
            previous = current;
            current = current.next;
        }

        previous.next = null;
        tail = previous;
        currentSize--;

        return current.data;
    }


    public E findAndRemove(E obj){
        Node<E> current = head, previous = null;

        // In an empty list current = null so we skip to the last line
        while(current != null){
            if(((Comparable<E>)obj).compareTo(current.data) == 0){

                // Beginning or single element
                if(current == head)
                    return removeFirst();

                // Ending of the list
                if(current == tail)
                    return removeLast();

                currentSize--;

                // Removing the reference to the node to delete
                previous.next = current.next;

                return current.data;
            }
            previous = current;
            current = current.next;
        }
        // Node not found
        return null;
    }

    public boolean contains(E obj){
        Node<E> current = head;

        while(current != null) {
            if(((Comparable<E>) obj).compareTo(current.data) == 0)
                return true;

            current = current.next;
        }
        return false;
    }

    public E peekFirst(){
        if(head == null)
            return null;

        return head.data;
    }

    public E peekLast(){
        if(tail == null)
            return null;

        return tail.data;
    }



}
下面是一个方便的图表,提醒您哪个类应该具有哪些函数:


欢迎来到Stackoverflow。你能告诉我们“不能编译”是什么意思吗?您的错误消息是什么?对不起,我的错,我的意思是list.addFirstWithTail(i)无法解析,如果我编写list,则无法访问我在LinkedList类中编写的方法。最后,我不能用for-each循环进行迭代,也就是说,如果我将列表声明为LinkedList而不是ListIterator,那么它就不能应用于类型。我可以访问这些方法,但仍然不能使用for-each循环。非常感谢,它现在工作正常。我会把你的答案标记为正确。如果你不介意,我还有一个问题:我不能在
LinkedList list=newlinkedlist()中更改变量列表的签名吗类似于
Iterable list=newlinkedlist()List myList=new ArrayList()中所做的那样工作?您可以将“列表”设置为可编辑类型,但如果您想使用特定于LinkedList的函数(addFirstWithTail(),等等),而这些函数不是为可编辑定义的,则需要将“列表”强制转换为可编辑类型,例如:((LinkedList)list)。addFirstWithTail(i);
package linkedlists;

public class Tester {

    public static void main(String[] args) {

        LinkedList<Integer> list = new LinkedList<>();
        int n = 10;

        for (int i = 0; i < n; i++)
            list.addFirstWithTail(i);


        int removedFirst = list.removeFirst();
        int removedLast = list.removeLast();

        for(int x : list){
            System.out.println(x);
        }
    }
}