簡體   English   中英

我如何從Java集獲取返回值 <String> 在java的另一個類中

[英]how i can get the return value from java set<String> in another class in java

我如何使用java set方法獲取其他類中的返回值。 我已經在任何機構中加入了我的程序,請給我打電話,告訴我我如何從其他課程中獲得返回值。

    import java.util.HashSet;
    import java.util.Set;

    public static Set<String> crunchifyPermutation(String str) {
        Set<String> crunchifyResult = new HashSet<String>();
        if (str == null) {
            return null;
        } else if (str.length() == 0) {
            crunchifyResult.add("");
            return crunchifyResult;
        }

        char firstChar = str.charAt(0);
        String rem = str.substring(1);
        Set<String> words = crunchifyPermutation(rem);
        for (String newString : words) {
            for (int i = 0; i <= newString.length(); i++) {
                crunchifyResult.add(crunchifyCharAdd(newString, firstChar, i));
            }
        }
        return crunchifyResult;
    }

    public static String crunchifyCharAdd(String str, char c, int j) {
        String first = str.substring(0, j);
        String last = str.substring(j);
        return first + c + last;
    }

}

我如何獲得我嘗試過的另一個類的返回值

crunchifyPermutation c = new crunchifyPermutation();
Set<String> z = cr.crunchifyPermutation(ring);

但它僅在[ ]返回如何獲取String中的所有值。

如果您想用逗號分隔值列表,則

import java.util.stream.Collectors;

// ...
Set<String> z = cr.crunchifyPermutation(ring);
String zAsString = z.stream().collect(Collectors.joining(", "));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM