Generate all permutations in Java [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Generating all permutations of a given string
I have an array of arbitrary length in Java, and I would like to generate all possible permutations of them. The easy way to do this for a fixed length would be a series of nested for loops, but because the array is of unknown length, that is not an option here. Is there a straightforward way to accomplish this in Java?

Use a recursive function, instead of loops. Each time you call the method should be on a smaller portion of the array and stop when length = 0. This link should help you design your function.

It may or may not be optimal as far as performance goes, but if you're looking for a way to do it with writing relatively little code and having it be clear and maintainable, you want a recursive method rather than nested loops.

Related

Finding number of Iterations [duplicate]

This question already has answers here:
What is the time complexity of java.util.Collections.sort() method?
(4 answers)
Closed 4 years ago.
I am trying to find the number of iterations an algorithm does when sorting.
How we find the number of iterations built in sort:Collections.sort does when sorting?
I don't think you can count the number of iterations at run time. (You could count them using a debugger, but I don't think that's what you mean.)
You can count the number of comparisons done by Collections.sort() by using the two-argument version of the method and passing your own comparator that counts how many times it is called. That's probably a more meaningful measure of sorting work than counting iterations.

how to split a list into a given number of sub-lists? [duplicate]

This question already has answers here:
Is there a common Java utility to break a list into batches?
(21 answers)
Closed 6 years ago.
I have a list that may be over 1000 strings, however I do not know how many exactly.
What is the best way to split this list into smaller lists without loosing any members of the list?
For example If I have a list of 1323 members, how can I best split it into 3 almost evenly sized lists?
I have seen the Guava and Commons way of splitting lists by the partition function, but that function will split the list into given size of chunks and not given number of groups (sub-lists).
Guava has a function Lists.partition which will do this for you
Usage:
Lists.partition(mylist, mylist.size()/3);

In Java, why use a array over an arrayList [duplicate]

This question already has answers here:
Why not always use ArrayLists in Java, instead of plain ol' arrays?
(5 answers)
Closed 8 years ago.
I've played around with arrays and arrayLists. Sorting is a breeze with arrayLists but I can not say the same with arrays. You have to make a for loop and filter the results and it's a pain. As far as I can see arrayList are just a better version of arrays. So why use arrays? Is there something they can do better.
Sorting with arrays and ArrayLists is equally easy: there are already implementations for sorting both of them:
Sorting ArrayLists
Sorting Arrays
The reasons for using one of the other are more of memory concerns, speed concerns or because your problems is better suited to be solved with one or the other.

checking a string in array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In Java, how can I test if an Array contains a certain value?
I want to check for a string S whether it is present in string array or not. Is there any direct method to do so ?
EDIT
If the answer excludes the use of List then that would be better.
There are a couple of ways to accomplish this using the Arrays utility class.
If the array is not sorted:
java.util.Arrays.asList(theArray).indexOf(o)
If the array is sorted, you can make use of a binary search for performance:
java.util.Arrays.binarySearch(theArray, o)
See here Where is Java's Array indexOf?
You can convert it to List and check: Arrays.asList(arrayVariable).contains(S)

how do I split string equation into its own array of strings? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How would I Evaluate a certain formula?
How would I split this formula into an array of characters each having their own number in the array:
a1+a2+a5*((a1+a6)*a3)
one I have added the spaces I am going to get column 1 because a1 will indicate column one and it will contain a number than I will add that to column 2. I am not allowed to use a tree or any of those other things just stacks and I have been asking that. But people keep telling me to use libraries and trees I am only in a 200 level course !
You need a grammar and a parser to do this in a general way. Something like this.

Categories

Resources