The method "combination" should make combination of input arrays. And I like to get this stream of combination and save it into a file.
public void writeDot() {
try (PrintStream out = new PrintStream(path, "UTF-8")) {
out.print("digraph {\n");
String[] arr = {"hyo", "ji", "yoo", "mi", "vi", "se", "ari"};
combination(arr, 2, 0, new String[2])
.stream()
.map(a -> Arrays.toString(a).join(" -> "))
.forEach(out::print);
out.println(";\n");
out.println("}");
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
Combination methods goes like this:
public List<String[]> combination(String[] arr, int len, int startPosition, String[] result) {
if (len == 0) {
//System.out.println(Arrays.toString(result));
return null;
}
for (int i = startPosition; i <= arr.length - len; i++) {
result[result.length - len] = arr[i];
combination(arr, len - 1, i + 1, result);
list.add(result);
}
return list;
}
Result I expected is:
digraph {
hyo -> ji;
ji -> hyo;
and so on..
}
But I only get:
digraph {
;
}
What's wrong with my code? please help me.
String.join
is a static method that accepts two parameters: delimiter and array of elements.
You pass no elements, so result is empty string.
Correct code is:
combination(arr, 2, 0, new String[2])
.stream()
.map(a->String.join(" -> ", a))
.forEach(out::print);