java


Algorithm for combinations


This is the Programm:
public List<List<Fieldmatrix>> permute(List<Fieldmatrix> fields, int start, List<Fieldmatrix> prefix) {
List<List<Fieldmatrix>> combinations = new ArrayList<>();
if (start >= fields.size()) {
combinations.add(prefix);
return combinations;
}
String field = fields.get(start).getFieldName();
if (fields.get(start).isMandatory() == false) {
combinations.addAll(combine(fields, start + 1, prefix));
}
List<Fieldmatrix> prefixWithField = new ArrayList<>(prefix);
prefixWithField.add(new Fieldmatrix (field, fields.get(start).isMandatory(), fields.get(start).getFieldtype(), new ArrayList<>()));
combinations.addAll(combine(fields, start + 1, prefixWithField));
return combinations;
}
My Idea was: For example I have an XML with these fields
Field1, true
Field2, true
Field3, false
Field4, false
Field5, false
The results are:
{Field1, Field2, Field3, Field4, Field5}
{Field1, Field2, , Field4, Field5}
{Field1, Field2, , , Field5}
{Field1, Field2, , , }
{Field1, Field2, Field3, , Field5}
{Field1, Field2, Field3, , }
{Field1, Field2, Field3, Field4, }
{Field1, Field2, , Field4, }
The fields have the value true or false. False fields can be deleted. So the program finds every possible combination of these fields.
Fieldmatrix:
public class Fieldmatrix {
private String fieldName;
private boolean mandatory;
private Type fieldtype;
private List<Fieldmatrix> list = new ArrayList<>();
public Fieldmatrix(String fieldName, boolean mandatory, Type fieldtype, List<Fieldmatrix> list){
this.fieldName = fieldName;
this.mandatory = mandatory;
this.fieldtype = fieldtype;
this.list = list;
}
//Getters and Setters
This program works only for "1-dimensional" Lists, this means if any field has no fields in his own list.
I want to update this code.
Now I have a structure like this:
Field 1, true
Field 2, true
Field 3, false
Subfield 1, true
Subfield 2, true
Subsubfield 1, false
Subfield 1 and Subfield 2 are in the list of Field 3. Subsubfield 1 is in Subfield 2.
How should I Change the code, so that works for more dimensional examples?
Has anybody an Idea?
Assuming the order doesn't matter, you can use a way to generate all combinations of optional (i.e. FALSE params in your case) as follows:
Let's say you have 3 optional params, and 2 compulsory params.
So you will have 3^2 = 9 combinations.
Now, all you have to do is iterate 9 times to generate all possible combinations.
Just a pseudo code:
Your options: ["A", "B", "C"]
3^2=9 possibilities.
for(i=0;i<9;i++){
for(j=0;j<options.length;j++){
if(j'th bit is set in 'i')
print(options[j]);
}
}
You can google to check if a particular bit is set or not.
Hope this is enough to get you started.
All the best.

Related Links

Formatting matrices to be read left to right
Spring model object becomes null on jsp
Android ffmpeg: Path with spaces
Joda Time to Date in the specified format
Regards how to check memory leak in my web application?
Missing return statement (Java): Calculating first 100 pentagonal number)
How to set classpath for ojdbc14.jar in netbeans?
How to get Icon for a specific format on mac
Which Canvas and Bitmap methods get scaled by dpi (Android)?
Reference HTTP headers in WSDL
Get date of previous and next week monday from a particular date [closed]
Logging is not working while implementing AOP aspect?
Adding a system dependency to Maven
Gradle Multiproject Build: How to include JAR of a WAR project into another WAR without creating pseudo projects
Spring scheduling task - run only once
What caused socket connections to be slow after Full GC?

Categories

HOME
java
pandas
osgi
dictionary
cplex
adb
spring-jdbc
cross-browser
yum
serverspec
ios-charts
paradox
vifm
reveal.js
modx-revolution
modelica
leiningen
lombok
systemc
invantive-sql
firefox-webextensions
nodatime
one-hot-encoding
java-3d
css-animations
kvc
realex-payments-api
microsoft-r
jndi
trading
sqlcipher
semantic-analysis
saas
webkitspeechrecognition
srcset
key-value-observing
.net-4.0
xquery-3.0
contact-form
perlin-noise
rotational-matrices
wpf-controls
hot-module-replacement
swift3.0.2
vapor
greenrobot-eventbus
hendrix
celery-task
sage-one
git-diff
r-forge
sqlbulkcopy
lync-client-sdk
webdriverjs
sonarlint-vs
cubes
gridpane
azure-sdk
libpng
rotativa
qpid
blackberry-10
dataview
pervasive-sql
execute
jms2
eclipse-clp
qdialog
angular-leaflet-directive
mmc
key-management
formatjs
operator-precedence
responsive-slides
sgen
aapt
funcunit
datagridviewcolumn
viadeo
mysqltuner
xsockets.net
.aspxauth
shellexecute
mylyn
interface-orientation
html-editor
anonymous-methods
jquery-knob
coderush
bluepill
ocx
ticoredatasync
buildr
removeclass
work-stealing
adk
libc++
gcj
servicehost
oncheckedchanged
datareader
suppress
zend-tool
boost-smart-ptr
geneva-server
lzh

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile