Both look like adding a node in a linked list. There is a serious problem in the void example, that you are assigning a value to a parameter; since Java™ is entirely pass-by-value, that will not alter the original reference, and you will be left with a null to produce nice NullPointerExceptions later on.
Some examples of bad design, that the LN class appears not to have all its fields private.
Joined: Feb 20, 2011
Below is my full code. For the insertOrder method I am tying to insert a set of integers in order using recursion. The following error results in relation to the method:
operator < cannot be applied to T,T (SEE METHOD BELOW). My original question was, Can i take this same method and convert it into a void type rather than returning a node?
Joined: Oct 13, 2005
You can only apply the < operator to numbers. That includes the primitives byte char double float int long and short, as well as their corresponding wrapper classes, but (I think) notBigDecimal and BigInteger. If you are trying to apply < to T, then you would have to make T implement Comparable and use the compareTo() method.
Instead of T it's T extends Comparable<? super T> and that bit in the if would be a.compareTo(b) < 0.
I see you have your node as an inner class; that way you gain access to its private fields. I still think you are better off putting addNext() removeNext() etc. methods inside the inner Node class.
And since "T extends Comparable<? super T>" looks complex I'll explain it to you.
You want your objects to be mutually comparable. That means that T must be bound to Comparable<T> - this ensures that each object has a method "compareTo(T)".
However, not all classes implement Comparable themselves; java.sql.Timestamp for instance extends java.util.Date which implements Comparable<Date>. Therefore, Timestamp implements Comparable<Date> too, not Comparable<Timestamp>. By using Comparable<? super T> you allow classes like Timestamp themselves - classes that are not just comparable to the class itself but to some super class as well.
I am not quite familiar with the concept, however, i have more than one Ts in the code presented, so where exactly should I put it, that is,public static <T extends Comparable<? super T>> void sort(List<T> list)