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.
smkJackstrawAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
System.out.println(theArray[index]);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.