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 如何获取length和isEmpty方法作为返回值,以获取用于循环链表的方法?_Java_Algorithm_Data Structures_Linked List_Circular List - Fatal编程技术网

Java 如何获取length和isEmpty方法作为返回值,以获取用于循环链表的方法?

Java 如何获取length和isEmpty方法作为返回值,以获取用于循环链表的方法?,java,algorithm,data-structures,linked-list,circular-list,Java,Algorithm,Data Structures,Linked List,Circular List,当我看到链表中从未使用过的两种方法时,我遇到了一个问题: 新的更新 public class CircularLinkedList { private ListNode last; private int length; private static class ListNode { private ListNode next; private final int data; public ListNode(int d

当我看到链表中从未使用过的两种方法时,我遇到了一个问题: 新的更新

public class CircularLinkedList {

    private ListNode last;
    private int length;

    private static class ListNode {
        private ListNode next;
        private final int data;

        public ListNode(int data) {
            this.data = data;
        }
    }

    public static void main(String[] args) {
        CircularLinkedList cll = new CircularLinkedList();
        CircularLinkedList insertBeg = new CircularLinkedList();
        CircularLinkedList insertEnd = new CircularLinkedList();

        System.out.println("creating a traverse list");
        cll.createCircularLinkedList();
        cll.display();
        System.out.println("Insert Starting Node");
        insertBeg.insertFirst(10);
        insertBeg.insertFirst(15);
        insertBeg.insertFirst(20);
        insertBeg.insertFirst(25);
        insertBeg.display();

        insertEnd.insertLast(25);
        insertEnd.insertLast(20);
        insertEnd.insertLast(15);
        insertEnd.insertLast(10);

        // I just need to print out the isEmpty amd length
        System.out.println(insertEnd.isEmpty());
        System.out.println(insertEnd.length());
        insertEnd.display();
    }

    public CircularLinkedList () {
        last = null;
        length = 0;
    }

    public void display() {
        if(last == null) {
            return;
        }

        ListNode first = last.next;
        while(first != last) {
            System.out.print(first.data + " --> ");
            first = first.next;
        }
        System.out.println(first.data);
    }

    public void insertFirst(int data) {
        ListNode temp = new ListNode(data);
        if (last == null) {
            last = temp;
        } else {
            temp.next = last.next;
        }
        last.next = temp;
        length++;
    }

    public void insertLast(int data) {
        ListNode temp = new ListNode(data);
        if (last == null) {
            last = temp;
            last.next = temp;
        } else {
            temp.next = last.next;
            last.next = temp;
            last = temp;
        }
        length++;
    }

    public int length () {
        ListNode temp = last.next;
        int count = length;
        while (temp != last) {
            count++;
            temp = temp.next;
        }
        return count;
    }

    public boolean isEmpty(){
        return length == 0;
    }

    public void createCircularLinkedList() {
        ListNode first = new ListNode(1);
        ListNode second = new ListNode(5);
        ListNode third = new ListNode(10);
        ListNode fourth = new ListNode(15);

        first.next = second;
        second.next = third;
        third.next = fourth;
        fourth.next = first;

        last = fourth;
    }
}
我试图通过使该方法无效来解决循环链表的这个问题。但是,我想测试一下,看看是否可以通过打印出length和isEmpty的值来实现该方法。有没有办法解决IntelliJ上的这个问题


该方法的返回值永远不会同时用于length和isEmpty。这只是一个警告,其他代码正在调用该方法,但忽略了结果


解决方案是修复其他代码。

因为消息告诉您有一个返回值,但您对它完全不做任何处理。您可以将返回值分配给变量:
boolean isListEmpty=insertEnd.isEmpty()然后打印该变量。。。或者您可以直接打印方法return,就像在
System.out.println(insertEnd.isEmpty())中一样@ohgodspeiders谢谢,当我意识到这一点时,我差点忘了使用System.out.println。