Is there a way for me to display the output of a Doubly Linked List class in JList of another class? Java
Since your class
for your linked list does output to the console, the first thing you need to learn is to try and separate your concerns. Your linked list may or may not be outputted at certain method calls and may or may not be outputted to the console or your JList
. Since you may intend (which already happened in your case) to reuse your doubly linked list in different use-cases, you need to implement your class
in a very agnostic manner, without any assumptions about outputting it on the console.
Now, what should your doubly linked list support in order to ensure that it will work in separate environments:
- instantiation
- element adding
- element removal
- element iteration using a callback
Take a look here for examples of callback: Callback functions in Java
Now, if you need to use your class at a console application or a swing application, you can specify a callback and call the element iteration.