无法在流Java中的forEach循环内打印

方法“ combination”应组合输入数组。而且我想获得这种组合流并将其保存到文件中。

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,new String[2])
                .stream()
                .map(a -> Arrays.toString(a).join(" -> "))
                .forEach(out::print);
        out.println(";\n");
        out.println("}");
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }
}

组合方法如下:

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;
}

我期望的结果是:

digraph {
hyo -> ji;
ji -> hyo;

and so on..
}

但我只能得到:

digraph {
;

}

我的代码有什么问题?请帮助我。

iCMS 回答:无法在流Java中的forEach循环内打印

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; } 是一个静态方法,它接受两个参数:定界符和元素数组。

您不传递任何元素,因此结果为空字符串。

正确的代码是:

map(join)
,

似乎您的组合方法存在一些问题,这些问题无济于事,因为您仅通过创建一个新的结果数组来递归调用它。相反,请尝试一种更好的组合方法-理想情况下,为此使用经过良好测试的库,例如apache commons或番石榴。

hobby = ''
hobbies = []
no_room_left = False
room = 3
count = 0
while not no_room_left:
    if count<room:
        hobby = str(input("Enter hobby"))
        hobbies.append(hobby)
        count+=1
print(hobbies)

此外,您将需要按照talex的描述更新SELECT STR_TO_DATE('2017-11-16 08:10:38.328037','%Y-%m-%d %H:%i:%s') FROM DUAL; 方法调用。

本文链接:https://www.f2er.com/1611951.html

大家都在问