I am new to Java and Stack Overflow and I have a question about permutation.
Method: I generate ArrayList with objects within an ArrayList. Each ArrayList has the size from 1 (minium 1 possible) to infinite and contains custom generated objects with an unique name-attribute.
Question: Now my question is how i can get the permutation of all possible combinations of objects from the first ArrayList to the lastArrayList (i guess we can say that's the x-axis) within my outer ArrayList (y-axis)?
Example: I try to draw a simple example:
ArrayList: 1.1 | 1.2 | 1.3ArrayList: 2.1ArrayList: 3.1 | 3.2
Here these ArrayLists are in the outer ArrayList (because of the unknown number of possible ArrayLists with objects). And the second number is to show the different objects. Let's say "get every possible combination to get from top to bottom".
Result: I want to get a result which looks like this:
- Combination: 1.1 | 2.1 | 3.1
- Combination: 1.1 | 2.1 | 3.2
- Combination: 1.2 | 2.1 | 3.1
- Combination: 1.2 | 2.1 | 3.2
- Combination: 1.3 | 2.1 | 3.1
- Combination: 1.3 | 2.1 | 3.2
Edit: Here the separator "|" stands for e.g. a slot in an ArrayList. The combinations shouldn't be writen in the console because i need to access every object of the permutation individually.
The best case would be if I can get each combination after the other because I want to check several conditions on each combination and only safe certain combinations in a further ArrayList or an .txt-file.
What i got so far:
I have found a code snippet which permutates ArrayLists with Strings within an ArrayList and returns a single ArrayList with the combined Strings.
public static ArrayList<String> combineAllCases(ArrayList<ArrayList<String>> totalList)
{
ArrayList<String> result = new ArrayList<String>(totalList.get(0));
for(int index = 1; index < totalList.size() ; index++)
{
result = (ArrayList<String>) combineTwoLists(result, totalList.get(index));
}
return result;
}
and
private static ArrayList<String> combineTwoLists(ArrayList<String> list1, ArrayList<String> list2)
{
ArrayList<String> result = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
for(String s1 : list1)
{
for(String s2: list2)
{
sb.setLength(0);
sb.append(s1).append("#").append(s2);
result.add(sb.toString());
}
}
return result;
}
The idea:
With this method, I could use a String split to get each object name per combination and could search for this name the old ArrayLists to get the object back.
The problem:
This method works only for a small number of ArrayLists within an ArrayList (e.g. like the example above). If a have e.g. 16 ArrayLists of the size of 7 in the outer ArrayList, I get an Error of "MemoryOutOfSpace".
So as mentioned the best case would be to get combination after combination and decide individually if I want to keep the combination or not (I guess I would save each combination in a .txt-file because it could be, that I want to keep every combination --> bypass the problem of a further "MemoryOutOfSpace"-Error).
Short summary:
Inner-ArrayLists with objects (size from 1 to unknown size).
Outer-ArrayList with the inner-ArrayLists (unknown size).
Wanted Output: every combination of objects from top to bottom.
Thanks in advance.