![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Newbie
Join Date: Jan 2006
Posts: 10
Rep Power: 0
![]() |
Thanks Arevos, you're extremely helpful. I'll have a look at this and try work it out further. I've never practiced recursion before but I'm beginning to see how it would work here.
Sean |
|
|
|
|
|
#12 |
|
Programmer
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0
![]() |
Sorry - been busy with my classes. Looks like you guys are doing ok though.
![]()
__________________
Java Blog |
|
|
|
|
|
#13 |
|
Newbie
Join Date: Jan 2006
Posts: 10
Rep Power: 0
![]() |
I've read the code and understand it. I don't know what I should put in the else block. I know there should be a recursive call back to group(). But should the arrays a and b that are sent back in recursive call be one element smaller? And should this element be removed from the head?
|
|
|
|
|
|
#14 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Well, look at it this way: you're taking the lists a and b and combining them to produce the output list. So you're moving elements from your input lists to your output. As your output grows, your input shrinks.
But this isn't a serial process. You don't want to create one output list, you want to create many of them, one for each possible combination of the input lists a and b. Let's take an example of how one, individual combination might be constructed. Let's say you have a and b defined so: a = {"A", "B", "C"}
b = {1, 2, 3}a[0]b[0] a[0]b[1] a[0]b[2] Now for the second element in our combination. We've used up A and 2, so we need to remove them from the lists, because we can only use each letter once in a combination. The sequence "A = 1, A = 2, B = 3", is, I assume, not a valid sequence because A appears twice. So with A and 2 removed from our lists: a = {"B", "C"}
b = {1, 3}Let's say we go with a[0]b[0], or "B = 1". Again, we remove "B" and 1 from our lists, and we get: a = {"C"}
b = {3}A = 2 B = 1 C = 3 a[0]b[0] a[0]b[1] a[0]b[2] a[0]b[0] a[0]b[1] ... a[0]b[n - 1] Some helpful commands you'll probably need: a.get(0); // get the first element of a a.get(j); // get the (j-1)th element of a List newA = new LinkedList(a); // make a copy of list a a.add(newElement); // add newElement onto the end of list a String e = a.remove(j); // remove the (j-1)th element of a and put it in e |
|
|
|
|
|
#15 |
|
Expert Programmer
|
I'm not trying to start a fight but for future reference please use code tags.
__________________
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|