英文:
Cannot print inside forEach loop in stream java
问题
public void writeDot() {
try (PrintStream out = new PrintStream(path, "UTF-8")) {
out.print("digraph {\n");
String[] arr = {"hyo", "ji", "yoo", "mi", "vi", "se", "ari"};
List<String[]> combinations = combination(arr, 2, 0, new String[2]);
for (String[] combination : combinations) {
out.println(combination[0] + " -> " + combination[1] + ";");
}
out.println("}\n");
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public List<String[]> combination(String[] arr, int len, int startPosition, String[] result) {
List<String[]> list = new ArrayList<>();
if (len == 0) {
list.add(result.clone()); // Clone the result array before adding
return list;
}
for (int i = startPosition; i <= arr.length - len; i++) {
result[result.length - len] = arr[i];
list.addAll(combination(arr, len - 1, i + 1, result));
}
return list;
}
英文:
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.
答案1
得分: 2
String.join
是一个静态方法,接受两个参数:分隔符和元素数组。
您没有传递任何元素,所以结果是空字符串。
正确的代码是:
combination(arr, 2, 0, new String[2])
.stream()
.map(a -> String.join(" -> ", a))
.forEach(out::print);
英文:
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);
答案2
得分: 0
代码部分不要翻译。以下是翻译好的内容:
似乎你的组合方法存在一些问题,这些问题并没有帮助你的情况,因为你在递归调用它时只创建了一个新的结果数组。相反,尝试使用更好的组合方法 - 最好使用经过充分测试的库,如Apache Commons或Guava。
此外,根据talex的描述,你需要更新map(join)
方法的调用。
英文:
It appears you have some issues with your combination method which are not helping your case because you're recursively calling it with only ever making one new result array. Instead, try a better combination method - ideally use a well tested library for this, like apache commons or guava.
public static List<String[]> makePairsFromArray(String[] arr) {
List<String[]> list = new ArrayList<>();
for(int i = 0; i < arr.length - 1; i++) {
for(int j = i + 1; j < arr.length; j++) {
String[] pair = new String[2];
pair[0] = arr[i];
pair[1] = arr[j];
list.add(pair);
String[] opp = new String[2];
opp[0] = arr[j];
opp[1] = arr[i];
list.add(opp);
}
}
return list;
}
Additionally, you will need to update the map(join)
method call as described by talex.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论