无法在 Java 的 forEach 循环中打印

huangapple go评论75阅读模式
英文:

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, &quot;UTF-8&quot;)) {
        out.print(&quot;digraph {\n&quot;);
        String[] arr = {&quot;hyo&quot;, &quot;ji&quot;, &quot;yoo&quot;, &quot;mi&quot;, &quot;vi&quot;, &quot;se&quot;, &quot;ari&quot;};
        combination(arr, 2, 0, new String[2])
                .stream()
                .map(a -&gt; Arrays.toString(a).join(&quot; -&gt; &quot;))
                .forEach(out::print);
        out.println(&quot;;\n&quot;);
        out.println(&quot;}&quot;);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }
}

Combination methods goes like this:

public List&lt;String[]&gt; 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 &lt;= 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 -&gt; ji;
ji -&gt; 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-&gt;String.join(&quot; -&gt; &quot;, 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&lt;String[]&gt; makePairsFromArray(String[] arr) {
    List&lt;String[]&gt; list = new ArrayList&lt;&gt;();

    for(int i = 0; i &lt; arr.length - 1; i++) {
        for(int j = i + 1; j &lt; 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.

huangapple
  • 本文由 发表于 2020年9月7日 17:52:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63775214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定