[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Looping through array using recursion

The code:
// Purpose: Loop through array using recursion  
public class Recursion
{
public static void main(String[] args)
{
// create array
String[] theArray =   {"Apples", "Oranges", "Grapes""};

//print array theArray
System.out.println("The array contains the elements:");
for (int i = 0; i < theArray.length-1; i++)
System.out.print(theArray[i] + ", ");
System.out.println(theArray[theArray.length-1]);
System.out.println();

printArray(theArray, 0);

System.out.println();
}

public static void printArray (Comparable[] theArray, int index)
{
if (index > theArray.length)
{
System.out.println("An index greater than the array bounds has been specified.");
System.out.println("Either a parameter was incorrectly set initially, ");
System.out.println("or the program has completed its recursive loop through the array.");
return;
}
else if (index < 0)
{
index = 0;
System.out.println("An index less than the array bounds was specified.  Index has been set to: " + index);
System.out.println("==\n");
}
else
{
// commented out because I dont want index but element at index...like theArray[i]
//System.out.println(index);
printArray(theArray, index+1);
}}}

My problem:
I'm looping through fine but can't figure out how to get the loop to print out the elements (at index) of the array w/o using a for loop.  

ie...oranges, grapes

I have figured out how to print the index as it loops by adding:
System.out.println(index);

note the commented out section of the method code.
0
smkJackstraw
Asked:
smkJackstraw
2 Solutions
 
CEHJCommented:
System.out.println(theArray[index]);
0
 
mudriCommented:
I hope this will do the work that you want:

public static void printArray (Comparable[] theArray, int index)
{
      if (index >= theArray.length)
      {
            System.out.println("An index greater than the array bounds has been specified.");
            System.out.println("Either a parameter was incorrectly set initially, ");
            System.out.println("or the program has completed its recursive loop through the array.");
            return;
      }
      else if (index < 0)
      {
            index = 0;
            System.out.println("An index less than the array bounds was specified.  Index has been set to: " + index);
            System.out.println("==\n");
      }
      else
      {
            // Print on enter
            System.out.println("Direct "+index+") "+ ((String[]) theArray)[index].toString());
            printArray(theArray, index+1);
      }
      // Print on way back
      System.out.println("Reverse "+index+") "+ ((String[]) theArray)[index].toString());
}
0
 
smkJackstrawAuthor Commented:
Thanks,  figured it out just as you answered.  LOL :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now