Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>At first glance, I thought you were going for more of a LISP style collection where you'd have a "head" element and a "tail" list. Tossing together some code gave me this:</p> <pre><code>package generic.list; import java.util.List; public class GenericList&lt;T&gt; { private T head; private GenericList&lt;T&gt; tail; public GenericList(List&lt;T&gt; initialList) { if ( !initialList.isEmpty() ) { head = initialList.get(0); if ( initialList.size() &gt; 1 ) { tail = new GenericList&lt;T&gt;(initialList.subList(1, initialList.size())); } } } public T getHead() { return head; } public GenericList&lt;T&gt; getTail() { return tail; } } </code></pre> <p>While this sort of structure can lead to some really great investigations of recursive algorithms, it really doesn't do a whole heck of a lot for you when you're trying to learn generics. That said, here's a quick test bed I wrote to make sure this actually worked:</p> <pre><code> package generic.list; import static org.junit.Assert.*; import java.util.Arrays; import org.junit.Test; public class GenericListTest { private GenericList l; @Test public void testConstructorNoElements() { l = new GenericList(Arrays.asList(new String[] {})); assertNull(l.getHead()); assertNull(l.getTail()); } @Test public void testConstructorOneElement() { l = new GenericList(Arrays.asList("One")); assertNotNull(l.getHead()); assertEquals("One", l.getHead()); assertNull(l.getTail()); } @Test public void testConstructorMultipleElements() { l = new GenericList(Arrays.asList("One", "Two", "Three")); assertNotNull(l.getHead()); assertEquals("One", l.getHead()); assertNotNull(l.getTail()); assertEquals(l.getTail().getHead(), "Two"); assertEquals(l.getTail().getTail().getHead(), "Three"); assertNull(l.getTail().getTail().getTail()); } } </code></pre> <p>That should at least give you a better idea of how to instantiate and utilize a generic class.</p> <p>You can find an online lesson at <a href="http://download.oracle.com/javase/tutorial/java/generics/index.html" rel="nofollow">http://download.oracle.com/javase/tutorial/java/generics/index.html</a>.</p> <p>The biggest thing to understand about generics is that, when you list "T" in the name of the class (as in "GenericList"), T becomes a class for the scope of this class. No classes outside of GenericList know what T is and GenericList doesn't even really know what it is - all it knows is that, everywhere you see a T, it'll be the same type as a T somewhere else. So, while GenericList doesn't necessarily know what it's storing, it does know that the type of "head" (T) is the same type as the objects being passed in via initialList.</p>
    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.
    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