당신은 결과 "수집"하는 정적 목록을 사용할 수 있습니다 : 당신이 목록을 만들 수 있습니다, 또는
static List<String> perms = new ArrayList<>();
public static void main(String[] args) throws IOException {
permutation("abc");
System.out.println(Arrays.toString(perms.toArray())); // prints [abc, acb, bac, bca, cab, cba]
}
public static void permutation(String str) {
permutation("", str);
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) {
perms.add(prefix);
}
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1)); // you had a bug in the last index on this line
}
}
, 인수로 전달 :
public static void main(String[] args) throws IOException {
List<String> perms = new ArrayList<>();
permutation("abc", perms);
System.out.println(Arrays.toString(perms.toArray())); // print [abc, acb, bac, bca, cab, cba]
}
public static void permutation(String str, List<String> perms) {
permutation("", str, perms);
}
private static void permutation(String prefix, String str, List<String> perms) {
int n = str.length();
if (n == 0) {
perms.add(prefix);
}
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1), perms);
}
}
나는 당신이 "리턴 타입을 만들자"는 의미를 알아 내기 힘들다. –
@ChrisMartin 그는 단지 'void'를 반환하는 대신 '순열'에서 무엇인가를 반환하기를 원한다는 의미입니다. 그러나, 나는 그것이 정확히 무엇인지 확신 할 수 없다. – Jashaszun
서블릿 내에서 나중에 사용되는 반환 유형을 객체의 arraylist에 넣는 것과 같이 – user1712095