如何从java中的循环列表中打印数据?

How to print data from a circular list in java?

我正在 java 中使用循环列表。我正在尝试打印列表中的数据,但没有得到所需的输出。我得到的输出为

CircularList@55f96302

CircularList@3d4eac69

CircularList@42a57993

请帮忙解决这个问题。

谢谢!!

import java.util.Iterator;


public class CircularList<T> implements Iterable<T> {

    private static class myIterator<T> implements Iterator<T> {
        int i = 0;
        CircularList<T> myList;
        Entry<T> iter;

        public myIterator(CircularList<T> list) {   
            iter = list.head;
            myList = list;
        }

        public boolean hasNext() {
            return !(myList.size() == i)
        }

        public T next() {
            //System.out.println("next");
            i++;
            T nextvalue = iter.value;
            //Entry<T> nextnode = new Entry<T>();
            iter = iter.next;               
            return nextvalue;
        }

        public void remove() {
            if(myList.size() == 1) {
                myList.head = null;
                return;
            }

            if(iter == myList.head) {
                myList.head = myList.head.next;
            }
            iter.prev.next = iter.next;
            iter.next.prev = iter.prev;
        }
    } // end myIterator

    private static class Entry<T> {
        Entry<T> next;
        Entry<T> prev;
        T value;
    }

    private Entry<T> head;

    public int size() {
        int index = 0;

        Entry<T> before = head.prev;
        Entry<T> after = head;
        //System.out.println(head.value);
        //System.out.println(after.value);

        do {
            before = after;
            after = after.next;
            index++;
        } while (after != head);

        return index;
    }

    public boolean empty() {
        //System.out.println(head == null);
        return head == null;        
    }

    public void append(T value) {

        Entry<T> newnode = new Entry<T>();
        newnode.value = value;

        if (empty()) {
            head = newnode;
            head.next = head;
            head.prev = head;
        }
        else {
            Entry<T> before = head.prev;
            Entry<T> after = head;

            newnode.prev = head.prev;
            newnode.next = head;
            head.prev.next = newnode;
            head.prev = newnode;    
        }
    }


    public void insert(int index, T value) {

        Entry<T> newnode = new Entry<T>();
        newnode.value = value;

        if (empty()) {
            head = newnode;
            head.next = head;
            head.prev = head;
        }
        else {
            Entry<T> before = head.prev;
            Entry<T> after = head;

            if(index == 0) {
                head = newnode;
            }
            else {
                while(index > 0) {
                    before = after;
                    after = after.next;
                    index--;
                }

                 newnode.prev = before;
                 newnode.next = after;
                 newnode.prev.next = newnode;
                 newnode.next.prev = newnode; 
            }
        }
    } // end insert()


    public void remove(int index) {

        Entry<T> before = head.prev;
        Entry<T> after = head;

        while(index > 0) {
            before = after;
            after = after.next;
            index--;
        }   

        after.prev.next = after.next;
        after.next.prev = after.prev;
    }


    public void prepend(T value) {
        insert(0, value);
    }


    public void appendList(CircularList<T> list) {

        if (empty()) {
            head = list.head;
            return;
        }
        if (list.empty()) return;

        Entry<T> a = list.head;
        Entry<T> b = list.head.prev;
        Entry<T> c = head;      
        Entry<T> d = head.prev;

        a.prev = d;
        d.next = a;

        b.next = c;
        c.prev = b;
    }


    @Override
    public Iterator<T> iterator() {
        return new myIterator(this);        

        //throw new UnsupportedOperationException("Not implemented yet.");
    }

    public static void toString(CircularList<Integer> list) {

        String sol = "";        

        //for (CircularList<Integer> list : data) {
            sol = sol +"\n"+ String.valueOf(list);            

        //}

        System.out.println(sol);
        //return sol;
    }
}

String.valueOf 没有得到数据。它只打印对象的文本表示,包括它在内存中存储的区域 (@55f96302)。这样的事情应该有效:

for(String str : list)
{
    System.out.println(str);
}

您的问题是您已将 toString 设为 static 方法。您还将其 return 类型设置为 void 而不是 String。你需要写一些非静态的东西,在你的 CircularList class 中带有签名 public String toString(),并让它 return 你想要输出的 String。我还建议使用 @Override 注释,以便编译器检查您的签名是否正确。

您需要从 CircularList class 中的 java.lang.Object 覆盖 toString()。 因为还没有完成,所以 java 打印 java.lang.Object.toString() 打印内存地址。 提醒一下,Java 中的每个对象都扩展了 java.lang.Object.

首先,我建议您不要以这种方式覆盖 toString() 方法。您的方法用于打印,而不是 'returning' 字符串对象。而且和原来的toString方法完全不同。无论如何,我猜你现在得到的结果是循环列表中对象的地址。

for(Integer data : list){
    system.out.println(data.toString());
}

而且您还必须像这样修改您的代码:)