aspose file tools*
The moose likes Beginning Java and the fly likes Recursion: returning a value/not returning a value(void) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Recursion: returning a value/not returning a value(void)" Watch "Recursion: returning a value/not returning a value(void)" New topic
Author

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

brian robertson
Greenhorn

Joined: Feb 20, 2011
Posts: 3




How could i re-organize this code so that the method is void instead of returning a value
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Feb 20, 2011
Posts: 3


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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
brian robertson
Greenhorn

Joined: Feb 20, 2011
Posts: 3


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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursion: returning a value/not returning a value(void)