Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recursion: returning a value/not returning a value(void)

 
brian robertson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




How could i re-organize this code so that the method is void instead of returning a value
 
Stephan van Hulst
Bartender
Pie
Posts: 5399
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Please tell us what the code is supposed to do in the first place, and what the LN class is supposed to be/do.
 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
brian robertson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?





 
Campbell Ritchie
Sheriff
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) not BigDecimal 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
brian robertson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic