Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×
User Journal

Journal Chacham's Journal: Chronicle: Had a problem conceptualizing recursion in Java

I'm reading Java: A Beginner's Guide by Herbert Schildt. Schildt really is good. The lessons are smooth, with small complete examples of everything, explanations, and learning in steps, that is, each chapter builds on what was learned in the past. It's not just a bunch of concepts thrown together.. Here's one case where the O'reilly book just didn't do the job. It was good, but not for learning (reviewing, perhaps.)

I'm typing in every example, skipping the comments though. Also, changing names when they use plurals. An array should be named num, not nums, because each member is an instance of a num. It acts as a collections of nums, but it is not what it is. It's the J/P thing again. In databases, which is J territory, it should clearly be singular. Each record is an instance of the singular object (table.) And, people who think of tables in the plural often come up with terrible deigns and write horrible queries. Their using the database to support a specific process (which always changes, anyway) and not to hold data. They never learn. But i digress. Programs are about getting something done, so, it is more likely it should be named in the plural. I guess i'm in the wrong here. Though, as my code is for me (as opposed to if i was on a team), i'm going to follow my own preference.

In the Self Test for Chapter 6, question 6 is: Write a recursive method that displays the contents of a string backwards. I hit a mental block with that yesterday and just couldn't get it right. I was amazed (read: horrified) that such a small thing could be so hard. I ought to be able to (know what i need to do to) write that in seconds. After some fumbling over char vs String, it was time to go home. Today i approached the code and fixed it in just a few minutes.

class test06
{
  static String backward(String a)
  {
    if(a.length() == 1) return a;

    return a.substring(a.length() - 1) + backward(a.substring(0, a.length() - 1));
  }

  public static void main(String arg[])
  {
    System.out.println(backward("abcdef"));
  }
}

When i first got the question, i misunderstood it. I saw his answer and realized i misread it, so i tried this. Compared to his answer, he cheats. He used .charAt() to print out one char at a time from within the method. Granted, the book does that at this point, but this one is truer. And, i need something to be proud about.

But why did it take me so long? At first, i assumed its because i'm not used to Java, recursion is silly in this case, and i don't usually do recursion. But that's not true. I had a problem conceptualizing it, its effective, and i do it occasionally in SQL. But there's the answer. I do it in SQL.

Recursive CTEs are a pain. While more versatile than Oracle's hierarchical queries (which have a number of their own benefits), they are also confusing to learn. At some point it clicks though, and then its just a matter of keeping things straight in your head. However, in SQL's recursion the inner most level is also the final level. Outside of SQL, the opposite is true.

It's convenient to have blame it on SQL, though i know it's not true. Embarrassing as it is, i hit a mental block on the concept. Nonetheless, SQL likely had something to do with my confusion. I love these "easy" tests.

This discussion has been archived. No new comments can be posted.

Chronicle: Had a problem conceptualizing recursion in Java

Comments Filter:

Somebody ought to cross ball point pens with coat hangers so that the pens will multiply instead of disappear.

Working...