Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing elements in a generic list
    primarykey
    data
    text
    <p>I wrote a linked list implementation for my java class earlier this year. This is a generic class called LList. We now have to write out the merge sort algorithm for a lab. Instead of creating a new List implementation that takes Ints, I decided to just reuse the generic list I had created before.</p> <p>The problem is how do I compare two generic objects? java wont let me do something like </p> <pre><code>if(first.headNode.data &gt; second.headNode.data) </code></pre> <p>So, my question is, is their a way to implement some sort of comparison function that will work on any type of data? I tried the following:</p> <pre><code> String one, two; one = first.headNode.data.toString(); two = second.headNode.data.toString(); if(first.headNode.data.compareTo(second.headNode.data) &lt; 0) { result.add(first.headNode.data); // remove head node. remove() takes care of list size. first.remove(1); } else { // If compareTo returns 0 or higher, second.headNode is lower or // equal to first.headNode. So it's safe to update the result // list result.add(second.headNode.data); second.remove(1); } </code></pre> <p>Which wont even work properly. I tested with the numbers 6 and 12, the above adds 12 to the result list.</p> <p>Relevant stuff:</p> <pre><code> private LList&lt;T&gt; mergeSort(LList&lt;T&gt; list) { LList&lt;T&gt; first = new LList(); LList&lt;T&gt; second = new LList(); if (list.length() == 1) { return list; } int middle = list.length() / 2; second.headNode = list.getNodeAt(middle + 1); second.length = list.length() - (middle); // Set first half to full list, then remove the "second" half. first.headNode = list.headNode; first.length = middle; first.getNodeAt(middle).next = null; // Get the splitted halves. first = mergeSort(first); second = mergeSort(second); return merge(first, second); } private LList&lt;T&gt; merge(LList&lt;T&gt; first, LList&lt;T&gt; second) { LList&lt;T&gt; result = new LList(); while((first.length &gt; 0) &amp;&amp; (second.length &gt; 0)) { // Ok, lets force toString to compare stuff since generics are a pain. String one, two; one = first.headNode.data.toString(); two = second.headNode.data.toString(); if(one.compareTo(two)) &lt; 0) { result.add(first.headNode.data); // remove head node. remove() takes care of list size. first.remove(1); } else { // If compareTo returns 0 or higher, second.headNode is lower or // equal to first.headNode. So it's safe to update the result // list result.add(second.headNode.data); second.remove(1); } } return result; } </code></pre> <p>NOTE: entire LList class can be found [here](<a href="http://rapidshare.com/files/219112739/LList.java.html" rel="nofollow noreferrer">http://rapidshare.com/files/219112739/LList.java.html</a> MD5: BDA8217D0756CC171032FDBDE1539478)</p>
    singulars
    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.
 

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