print() function that prints the contents of each element of your list

社会主义新天地 提交于 2019-12-02 09:52:17

We will need to see inside the class of LList.java... but for now i am going to assume LList extends List [or ArrayList, etc..]

public void print
{
     for(int i = 0; i < this.size(); i++)  //this really depends on how you store your list
          System.out.println(this.get(i));
}

This all depends on how your LList.java looks though... [this.size()] refers to the List or ArrayList class [if you extended it...].

If you are not extending List or something along those lines, you could always do:

public void print
{
     for(int i = 0; i < storingArray.size(); /*or .length*/ i++)
          System.out.println(storingArray.get(i)); /*or storingArray[i]*/
}

But as always, you could take the easy way and just do:

list.foreach(System.out::println); //must have Java 8.

Revised Answer based on your comments:

public void print() {
        Link<E> currentNode  = head; //Sets starting node to first node in list
        while (currentNode != tail) { //Checks if current node is equal to last node
            System.out.println(currentNode.element()); //Prints currentNodes's element
            currentNode = currentNode.next(); //Sets currentNode to next node in list
        }
        System.out.println(tail.element()); //Prints last node in list      
}

Note: Some of your comments in you Link<E>code don't match what your functions are actually doing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!