This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Logic with a looping method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Logic with a looping method" Watch "Logic with a looping method" New topic

Logic with a looping method

S Chan
Ranch Hand

Joined: Jul 25, 2011
Posts: 51
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

Joined: Oct 13, 2005
Posts: 37950
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.
I agree. Here's the link:
subject: Logic with a looping method
Similar Threads
extreme beginner question
algorithm problem with recursion
Collections and Generics Help
I need some help with this quiz!