5 Replies Latest reply on Feb 17, 2008 1:39 AM by 807601

# Array Help

How can I take a string of numbers, and use a recursive method to put these numbers into an array?

String x = 1234

array[0] = 1
array[1] = 2
array[2] = 3
aray [3] = 4
• ###### 1. Re: Array Help
Look at the String class in API document.
It depends on which method to use depending on what kind of array you want to have.
• ###### 2. Re: Array Help
Why recursive? An iterative approach is, IMHO, much more appropriate.
• ###### 3. Re: Array Help
Of Course I know that, but we have to write a program, and it all has to be recursivly.. That is why I asked!
• ###### 4. Re: Array Help
Think about a tail recursive approach.

For example, the word 'hello' could be thought of as h + ello
and ello could be thought of as e + llo

etc

That should be enough to get you started.
If you have any problems post the code you have so far rather than asking for the answer. As trite as it sounds you'll pick up more by having a go yourself first.
• ###### 5. Re: Array Help
I dont want to spoon feed you. I've given you an example how it can be done using characters. Use something similar for numbers.
``````public class Test15 {
static String str="Paul";
static char[] c = new char[str.length()];
public static void main(String[] args) {
toArray(0);
for (int i = 0; i < c.length; i++) {
System.out.println(c);
}
}
static void toArray(int i){
while(str.length()!=i){
c[i]=str.charAt(i);
i++;
toArray(i);
}
}
}Edited by: The_Matrix on Feb 16, 2008 5:38 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ``````