Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't, if you keep things like they are now. As far as <code>ListR</code> knows, it contains <code>Object</code>s, and <code>Object</code>s do not contain any particular information. They have a method <code>toString</code>, that you can override, but it's not suggested to use it for presentation purposes.</p> <h3>Option 1: Internal interface</h3> <p>What you can do, if you're allowed to change things a bit, is to force the contents of <code>ListR</code> to implement an interface, such as</p> <pre><code>public interface Printable { String printedForm(); } </code></pre> <p>and then</p> <pre><code>class Product implements Printable{ //TODO: Whatever you want @Override public String printedForm() { //TODO return a pretty printed form of the product. //e.g. return this element's name } } class ListR&lt;T extends Printable&gt;{ //... public void print(){ Node a=first; while(curr!=null){ //You can now invoke printedForm, because you forced T to implement Printable System.out.println(a.printedForm()); a=a.next; } } } </code></pre> <h3>Option 2: external formatter</h3> <p>If you don't want / can't change the implementation of Product, or if you want to implement a more flexible style of printing, you can use an external <code>Formatter</code> object, that will format your products.</p> <pre><code>//Unmodified class Product { } interface ObjectFormatter&lt;T&gt; { String stringForm(T obj); } class ProductFormatter implements ObjectFormatter&lt;Product&gt; { @Override public String stringForm(Product obj) { //TODO return product name or whatever you want } } class ListR&lt;T&gt; { private final ProductFormatter&lt;? super T&gt; fmt; public ListR(ProductFormatter&lt;? super T&gt; formatter) { this.fmt = formatter; } //... public void print(){ Node a=first; while(curr!=null){ System.out.println(fmt.stringForm(a.elem)); a=a.next; } } } </code></pre> <p>Then, when you create the list:</p> <pre><code>ListR&lt;Product&gt; list = new ListR&lt;&gt;(new ProductFormatter()); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload