• Post Reply Bookmark Topic Watch Topic
  • New Topic

Permutation/Combination help please  RSS feed

 
Michael Otto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got an Arraylist called listOfStringArrays which holds a series of String []. I've been working to come up with an algorithm that will output all combinations of string arrays according to their positions.
For Example:


Some sample output would like this:
Hello, Bat, Martha
Cat, Bat, Martha
Dog, Bat, Martha
Hello, Cow, Martha
Cat, Cow, Martha
Dog, Cow, Martha.....

My problem is that I don't know how many string arrays will be in a listOfStringArrays and the String[]s can be any size. I've been working on this for more than a day and feel like I am missing something fundamental, it seems it would be easy to do.
I would appreciate any suggestions on structuring this algorithm.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Recursion is probably the easiest way to do something like this...

Henry
 
Michael Otto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply Henry. The problem I have with recursion is stack overflow, otherwise I think you're right.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stack overflow with a small collection like that probably means you haven't set up your base condition correctly. You need to stop when you get to 0 elements.
 
Sandeep Kumar Jakkaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with Java .....

You find out a method ...which can create the combination of integers between 0 and number of arraysize ....

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!