1. Home
  2. Computing & Technology
  3. Java

Going Around and Around

By Paul Leahy, About.com

Recursion Solution

I should point out that although recursive code may look nifty, it tends not be the most efficient way to achieve a programming task. Just think about how many String objects are being created for this question! Still, it's fun to know that you can send your methods spinning if you really feel like it.

Here's my version of the program:

public class RecursivePattern {

  public static void main(String[] args) {

    System.out.println(getPattern("OOOOOOOOOX"));

  }

  private static String getPattern(String pattern)
  {
    if (pattern.contains("OO") == false)
    {
      return pattern;
    }
    else
    {
      return pattern + "\n" + getPattern(pattern.replace("OX", "XX"));
    }
  }
}
Explore Java
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Java
  4. Programming Exercises
  5. Beginner Level
  6. Going Around and Around>

©2009 About.com, a part of The New York Times Company.

All rights reserved.