<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Arrays and Recursion' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Arrays and Recursion' posted on the 'Java' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Fri, 24 May 2013 23:55:11 -0700</pubDate>
    <lastBuildDate>Fri, 24 May 2013 23:55:12 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Arrays and Recursion</title>
      <link>http://www.programmersheaven.com/mb/java/352420/352420/arrays-and-recursion/</link>
      <description>An array containting strings of actual written code needs to be formatted.  I'm not sure how to even recursively call a method so it prints the contents of an array, let alone format it's text.  Not sure if I even put my methods together properly.  The code only prints the first index value of the array, no recursion takes place, and the counter variable value isn't increasing.  How do I recursively call the method?  Can anyone help?&lt;br /&gt;
&lt;br /&gt;
public static void indent(String[] code){&lt;br /&gt;
&lt;br /&gt;
	int counter = 0;&lt;br /&gt;
	String spaces = "   ";&lt;br /&gt;
	&lt;br /&gt;
        System.out.print(indent(code, counter++, spaces));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static String indent(String[] code, int counter, String spaces){&lt;br /&gt;
&lt;br /&gt;
	String tmpStr1,&lt;br /&gt;
		   tmpStr2;&lt;br /&gt;
&lt;br /&gt;
		if(counter == 0){&lt;br /&gt;
			return code[counter];&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		tmpStr1 = code[counter - 1];&lt;br /&gt;
		tmpStr2 = code[counter];&lt;br /&gt;
&lt;br /&gt;
		if(tmpStr1.charAt(tmpStr1.length() - 1) == '{' &amp;amp;&amp;amp; counter &amp;lt; code.length - 1){&lt;br /&gt;
			code[counter] = spaces + tmpStr2;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
	return code[counter];&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/java/352420/352420/arrays-and-recursion/</guid>
      <pubDate>Wed, 17 Jan 2007 11:24:43 -0700</pubDate>
      <category>Java</category>
    </item>
    <item>
      <title>Re: Arrays and Recursion</title>
      <link>http://www.programmersheaven.com/mb/java/352420/352422/re-arrays-and-recursion/#352422</link>
      <description>: An array containting strings of actual written code needs to be formatted.  I'm not sure how to even recursively call a method so it prints the contents of an array, let alone format it's text.  Not sure if I even put my methods together properly.  The code only prints the first index value of the array, no recursion takes place, and the counter variable value isn't increasing.  How do I recursively call the method?  Can anyone help?&lt;br /&gt;
: &lt;br /&gt;
: public static void indent(String[] code){&lt;br /&gt;
: &lt;br /&gt;
: 	int counter = 0;&lt;br /&gt;
: 	String spaces = "   ";&lt;br /&gt;
: 	&lt;br /&gt;
:         System.out.print(indent(code, counter++, spaces));&lt;br /&gt;
: }&lt;br /&gt;
: &lt;br /&gt;
: public static String indent(String[] code, int counter, String spaces){&lt;br /&gt;
: &lt;br /&gt;
: 	String tmpStr1,&lt;br /&gt;
: 		   tmpStr2;&lt;br /&gt;
: &lt;br /&gt;
: 		if(counter == 0){&lt;br /&gt;
: 			return code[counter];&lt;br /&gt;
: 		}&lt;br /&gt;
: &lt;br /&gt;
: 		tmpStr1 = code[counter - 1];&lt;br /&gt;
: 		tmpStr2 = code[counter];&lt;br /&gt;
: &lt;br /&gt;
: 		if(tmpStr1.charAt(tmpStr1.length() - 1) == '{' &amp;amp;&amp;amp; counter &amp;lt; code.length - 1){&lt;br /&gt;
: 			code[counter] = spaces + tmpStr2;&lt;br /&gt;
: 		}&lt;br /&gt;
: &lt;br /&gt;
: 	return code[counter];&lt;br /&gt;
: &lt;br /&gt;
: }&lt;br /&gt;
: &lt;br /&gt;
: }&lt;br /&gt;
: &lt;br /&gt;
Recursions and arrays is quite simple. A recursive function calls itself until a certain condition is met. In case of an array the condition is nearly always the end of the array. In pseudo-code it looks something like this:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
recurseExample(Array a, int counter) {
  if (counter &amp;lt; a.length) {
    Do something with a[counter]
    recurseExample(a, counter++); // recursiveness
  } else {
    // a[counter] is invalid, since counter &amp;gt;= a.length
  }
}
&lt;/pre&gt;&lt;br /&gt;
This example "walks" through the array from some point to the end. For example:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
  recurseExample(a, 2);
&lt;/pre&gt;&lt;br /&gt;
processes the array from the third element to the last.&lt;br /&gt;
The statement "Do something with a[counter]" can be calls to another method, which performs the processing, or it can be code included in the if-then statement.&lt;br /&gt;
Here is a full working example of a recursive function:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
int recursiveSum(int[] numbers, int index) {
  int result;
  if (index &amp;lt; numbers.length) {
    result = numbers[index]; // processing
    result = result + recursiveSum(numbers, index++); // recursive call
  } else {
    result = 0
  }
  return result;
}
&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/java/352420/352422/re-arrays-and-recursion/#352422</guid>
      <pubDate>Wed, 17 Jan 2007 12:35:59 -0700</pubDate>
      <category>Java</category>
    </item>
    <item>
      <title>Re: Arrays and Recursion</title>
      <link>http://www.programmersheaven.com/mb/java/352420/352480/re-arrays-and-recursion/#352480</link>
      <description>Here's the code I have so far.  It does what it's supposed to do, I did have a bit of help with it.  Could I get anyones opinion?  Also one question about arrays and recursion.  Is it even possible to get rid of a for loop (when it comes to printing index values of an array), and get the values of an array to print to screen using recursion?&lt;br /&gt;
  &lt;br /&gt;
&lt;pre class="sourcecode"&gt;
import javax.swing.JOptionPane;

 public class Recursion {

 	public static void main (String args[]){

	   String[] code = {
	      "public class Test {",
	      "public static void main(String[] args) {",
	      "String s = \"Countdown: \";",
	      "",
	      "for (int i = 1; i &amp;lt;= 20; i++) {",
	      "System.out.print(i);",
	      "if (i &amp;lt; 20) {",
	      "System.out.print(\",\")",
	      "}",
	      "System.out.print(\"...\")",
	      "}",
	      "",
	      "System.out.println();",
	      "}",
	      "}"
	   };

	   for(int i = 0; i &amp;lt; code.length; i++){
	     System.out.println(code[i]);
	   }

	   System.out.println("===");

	   code = indent(code,0,0); 

	   for(int i = 0; i &amp;lt; code.length; i++){
	      System.out.println(code[i]);
       }
}


public static String[] indent(String[] code, int counter, int spaces){

	String pad = "";

	  if(code[counter].length() == 0){
		  counter++;
	  }

	  if(code[counter].charAt(0) == '}'){

	      spaces -=3;

      }

		for(int i = 0; i &amp;lt; spaces; i++){

			pad +=" ";

		}

		code[counter] = pad + code[counter];

	  if(code[counter].charAt(code[counter].length() - 1) == '{'){

		  spaces +=3;

	   }

		if(counter &amp;lt; code.length-1) indent(code,counter+1,spaces);
    	return code;

}
}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Zibadian,&lt;br /&gt;
&lt;br /&gt;
I also wanted to thank you for all of your help, i'm not sure if you've realised that you have helped me before, a couple of months ago, many times.  I wasn't able to voice my appreciation because I was out of the country for some time.  But I really appreciate all the help you have given me, and value your opinion when it comes to code I write.  Thanks again, and i'm sure i'll have some posts in the future and hope to hear back from you.  Hope you get this.&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/java/352420/352480/re-arrays-and-recursion/#352480</guid>
      <pubDate>Thu, 18 Jan 2007 12:28:08 -0700</pubDate>
      <category>Java</category>
    </item>
    <item>
      <title>Re: Arrays and Recursion</title>
      <link>http://www.programmersheaven.com/mb/java/352420/352482/re-arrays-and-recursion/#352482</link>
      <description>: Here's the code I have so far.  It does what it's supposed to do, I did have a bit of help with it.  Could I get anyones opinion?  Also one question about arrays and recursion.  Is it even possible to get rid of a for loop (when it comes to printing index values of an array), and get the values of an array to print to screen using recursion?&lt;br /&gt;
:   &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
: import javax.swing.JOptionPane;
: 
:  public class Recursion {
: 
:  	public static void main (String args[]){
: 
: 	   String[] code = {
: 	      "public class Test {",
: 	      "public static void main(String[] args) {",
: 	      "String s = \"Countdown: \";",
: 	      "",
: 	      "for (int i = 1; i &amp;lt;= 20; i++) {",
: 	      "System.out.print(i);",
: 	      "if (i &amp;lt; 20) {",
: 	      "System.out.print(\",\")",
: 	      "}",
: 	      "System.out.print(\"...\")",
: 	      "}",
: 	      "",
: 	      "System.out.println();",
: 	      "}",
: 	      "}"
: 	   };
: 
: 	   for(int i = 0; i &amp;lt; code.length; i++){
: 	     System.out.println(code[i]);
: 	   }
: 
: 	   System.out.println("===");
: 
: 	   code = indent(code,0,0); 
: 
: 	   for(int i = 0; i &amp;lt; code.length; i++){
: 	      System.out.println(code[i]);
:        }
: }
: 
: 
: public static String[] indent(String[] code, int counter, int spaces){
: 
: 	String pad = "";
: 
: 	  if(code[counter].length() == 0){
: 		  counter++;
: 	  }
: 
: 	  if(code[counter].charAt(0) == '}'){
: 
: 	      spaces -=3;
: 
:       }
: 
: 		for(int i = 0; i &amp;lt; spaces; i++){
: 
: 			pad +=" ";
: 
: 		}
: 
: 		code[counter] = pad + code[counter];
: 
: 	  if(code[counter].charAt(code[counter].length() - 1) == '{'){
: 
: 		  spaces +=3;
: 
: 	   }
: 
: 		if(counter &amp;lt; code.length-1) indent(code,counter+1,spaces);
:     	return code;
: 
: }
: }
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: Zibadian,&lt;br /&gt;
: &lt;br /&gt;
: I also wanted to thank you for all of your help, i'm not sure if you've realised that you have helped me before, a couple of months ago, many times.  I wasn't able to voice my appreciation because I was out of the country for some time.  But I really appreciate all the help you have given me, and value your opinion when it comes to code I write.  Thanks again, and i'm sure i'll have some posts in the future and hope to hear back from you.  Hope you get this.&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
Here's a method, which prints an array recursively:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
void recursePrint(String[] stringArray, int index) {
  if (index &amp;lt; stringArray.length) {
    System.out.println(stringArray[index]);
    recursePrint(stringArray, index++);
  }
}
&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/java/352420/352482/re-arrays-and-recursion/#352482</guid>
      <pubDate>Thu, 18 Jan 2007 12:35:27 -0700</pubDate>
      <category>Java</category>
    </item>
    <item>
      <title>Re: Arrays and Recursion</title>
      <link>http://www.programmersheaven.com/mb/java/352420/352490/re-arrays-and-recursion/#352490</link>
      <description>When I implement the method as follows,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
 import javax.swing.JOptionPane;

 public class A1Q3 {

 	public static void main (String args[]){

	   String[] code = {
	      "public class Ick {",
	      "public static void main(String[] args) {",
	      "String s = \"Countdown: \";",
	      "",
	      "for (int i = 1; i &amp;lt;= 10; i++) {",
	      "System.out.print(i);",
	      "if (i &amp;lt; 10) {",
	      "System.out.print(\",\")",
	      "}",
	      "System.out.print(\"...\")",
	      "}",
	      "",
	      "System.out.println();",
	      "}",
	      "}"
	   };

	   recursePrint(code,0);

	   //for(int i = 0; i &amp;lt; code.length; i++){
	   //  System.out.println(code[i]);
	   //}

	   System.out.println("===");

	   code = indent(code,0,0);

	   recursePrint(code,0);
	   //for(int i = 0; i &amp;lt; code.length; i++){
	    //  System.out.println(code[i]);
       //}
}


public static String[] indent(String[] code, int counter, int spaces){

	String pad = "";

	  if(code[counter].length() == 0){
		  counter++;
	  }

	  if(code[counter].charAt(0) == '}'){

	      spaces -=3;

      }

		for(int i = 0; i &amp;lt; spaces; i++){

			pad +=" ";

		}

		code[counter] = pad + code[counter];

	  if(code[counter].charAt(code[counter].length() - 1) == '{'){

		  spaces +=3;

	   }

		if(counter &amp;lt; code.length-1) indent(code,counter+1,spaces);
    	return code;

}

public static void recursePrint(String[] stringArray, int index) {

  if (index &amp;lt; stringArray.length - 1) {

    System.out.println(stringArray[index]);

    recursePrint(stringArray, index++); //line 91

  }

}

}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
It prints the first index value over and over again, then just says "at A1Q3.recursePrint(A1Q3.java : 91)", which means the problems at line 91 where I call the recursion for printing the values.  I'm not sure what to do, the code looks right...&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/java/352420/352490/re-arrays-and-recursion/#352490</guid>
      <pubDate>Thu, 18 Jan 2007 14:11:18 -0700</pubDate>
      <category>Java</category>
    </item>
    <item>
      <title>Re: Arrays and Recursion</title>
      <link>http://www.programmersheaven.com/mb/java/352420/352491/re-arrays-and-recursion/#352491</link>
      <description>: When I implement the method as follows,&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;
:  import javax.swing.JOptionPane;
: 
:  public class A1Q3 {
: 
:  	public static void main (String args[]){
: 
: 	   String[] code = {
: 	      "public class Ick {",
: 	      "public static void main(String[] args) {",
: 	      "String s = \"Countdown: \";",
: 	      "",
: 	      "for (int i = 1; i &amp;lt;= 10; i++) {",
: 	      "System.out.print(i);",
: 	      "if (i &amp;lt; 10) {",
: 	      "System.out.print(\",\")",
: 	      "}",
: 	      "System.out.print(\"...\")",
: 	      "}",
: 	      "",
: 	      "System.out.println();",
: 	      "}",
: 	      "}"
: 	   };
: 
: 	   recursePrint(code,0);
: 
: 	   //for(int i = 0; i &amp;lt; code.length; i++){
: 	   //  System.out.println(code[i]);
: 	   //}
: 
: 	   System.out.println("===");
: 
: 	   code = indent(code,0,0);
: 
: 	   recursePrint(code,0);
: 	   //for(int i = 0; i &amp;lt; code.length; i++){
: 	    //  System.out.println(code[i]);
:        //}
: }
: 
: 
: public static String[] indent(String[] code, int counter, int spaces){
: 
: 	String pad = "";
: 
: 	  if(code[counter].length() == 0){
: 		  counter++;
: 	  }
: 
: 	  if(code[counter].charAt(0) == '}'){
: 
: 	      spaces -=3;
: 
:       }
: 
: 		for(int i = 0; i &amp;lt; spaces; i++){
: 
: 			pad +=" ";
: 
: 		}
: 
: 		code[counter] = pad + code[counter];
: 
: 	  if(code[counter].charAt(code[counter].length() - 1) == '{'){
: 
: 		  spaces +=3;
: 
: 	   }
: 
: 		if(counter &amp;lt; code.length-1) indent(code,counter+1,spaces);
:     	return code;
: 
: }
: 
: public static void recursePrint(String[] stringArray, int index) {
: 
:   if (index &amp;lt; stringArray.length - 1) {
: 
:     System.out.println(stringArray[index]);
: 
:     recursePrint(stringArray, index++); //line 91
: 
:   }
: 
: }
: 
: }
: &lt;/pre&gt;&lt;br /&gt;
: &lt;br /&gt;
: It prints the first index value over and over again, then just says "at A1Q3.recursePrint(A1Q3.java : 91)", which means the problems at line 91 where I call the recursion for printing the values.  I'm not sure what to do, the code looks right...&lt;br /&gt;
: &lt;br /&gt;
Try printing the index instead to see if that increases ok. It should then output the numbers 0 to n, where n is the length-2 of the array.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/java/352420/352491/re-arrays-and-recursion/#352491</guid>
      <pubDate>Thu, 18 Jan 2007 14:37:58 -0700</pubDate>
      <category>Java</category>
    </item>
    <item>
      <title>Re: Arrays and Recursion</title>
      <link>http://www.programmersheaven.com/mb/java/352420/352549/re-arrays-and-recursion/#352549</link>
      <description>&lt;strong&gt;&lt;span style="color: Red;"&gt;This message was edited by circuz_phreak at  2007-1-19 12:41:31&lt;/span&gt;&lt;/strong&gt;&lt;hr /&gt;&lt;br /&gt;
Got it working, changed it to:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
public static void recursePrint(Object[] stringArray, int index) {

   System.out.println(stringArray[index]);
      
      if (index &amp;lt; stringArray.length - 1) {    
        recursePrint(stringArray, index + 1);
      }

&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Program works great, thanks!!!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/java/352420/352549/re-arrays-and-recursion/#352549</guid>
      <pubDate>Fri, 19 Jan 2007 12:41:14 -0700</pubDate>
      <category>Java</category>
    </item>
  </channel>
</rss>