Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logic with a looping method

 
S Chan
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am stuck with the logic when writing a private method within a class. I hope someone may help.

Given that I have a list of string as a parameter(List<String>), e.g.
"a", "b", "c", "d", "e"

I want to write a self-calling looping private method which will return me all the unique positioning combination of elements in the list. The ideal return type is List<List<String>>, the returned result of the above list would be:
"a", "b", "c", "d", "e"
"a", "b", "c", "e", "d"
"a", "b", "d", "c", "e"
"a", "b", "d", "e", "c"
"a", "b", "e", "d", "c"
"a", "b", "e", "c", "d"
"a", "c", "b", "d", "e"
"a", "c", "b", "e", "d"
"a", "c", "d", "b", "e"
"a", "c", "d", "e", "b"
"a", "c", "e", "d", "b"
"a", "c", "e", "b", "d"
"a", "d", "c", "b", "e"
"a", "d", "c", "e", "b"
"a", "d", "b", "c", "e"
"a", "d", "b", "e", "c"
"a", "d", "e", "b", "c"
"a", "d", "e", "c", "b"
"a", "e", "c", "d", "b"
"a", "e", "c", "b", "d"
"a", "e", "d", "c", "b"
"a", "e", "d", "b", "c"
"a", "e", "b", "d", "c"
"a", "e", "b", "c", "d"
"b", "a", "c", "d", "e"
"b", "a", "c", "e", "d"
:
:
:

Another question, other than a looping method, I wonder if there is any other more efficient solution for this problem.

I hope I am making my question clear as I don't express myself well enough usually.

Thank you every contributor in advance!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find a copy of Horstmann and Cornell Core Java2, which you will find reviewed on our books pages, and somewhere in there they show an example of such an anagram program with recursion.
Agree that <List><List><String>> is a suitable data structure, but not the only possibility.

Beware. The number of combinations is equal to n! where n is the number of letters involved, so the size of the output increases rapidly with the number of letters.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic