This discussion is archived
5 Replies Latest reply: Feb 16, 2008 5:39 PM by 807601

# Array Help

Currently Being Moderated
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
Currently Being Moderated
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
Currently Being Moderated
Why recursive? An iterative approach is, IMHO, much more appropriate.
• ###### 3. Re: Array Help
Currently Being Moderated
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
Currently Being Moderated
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
Currently Being Moderated
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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ``````