• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




How could i re-organize this code so that the method is void instead of returning a value
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

reply
    Bookmark Topic Watch Topic
  • New Topic