2014-09-03 2 views
0

주어진 문자열에 대한 순열을 생성하는 다음 재귀 메서드가 있습니다. arraylist에서 생성 된 문자열에 대한 반환 유형을 만들려고합니다. 특히 JSP 페이지에 출력을 출력하려고합니다.반환 형식의 재귀 메서드 만들기

public static void permutation(String str) { 
     permutation("", str); 
} 

private static void permutation(String prefix, String str) { 
    int n = str.length(); 
    if (n == 0) System.out.println(prefix); 
    else { 
      for (int i = 0; i < n; i++) 
     permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+ n)); 
     } 
} 
+0

나는 당신이 "리턴 타입을 만들자"는 의미를 알아 내기 힘들다. –

+0

@ChrisMartin 그는 단지 'void'를 반환하는 대신 '순열'에서 무엇인가를 반환하기를 원한다는 의미입니다. 그러나, 나는 그것이 정확히 무엇인지 확신 할 수 없다. – Jashaszun

+0

서블릿 내에서 나중에 사용되는 반환 유형을 객체의 arraylist에 넣는 것과 같이 – user1712095

답변

0

당신은 결과 "수집"하는 정적 목록을 사용할 수 있습니다 : 당신이 목록을 만들 수 있습니다, 또는

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); 
    } 
} 
+0

-1과 같이 반환 유형을 사용하고자합니다. 전역 변수를 사용하여 메소드의 반환 값을 생성/저장하는 것은 정말 나쁜 생각입니다. – JimN

+1

@JimN 먼저 정적 멤버를 사용하지 않고 두 번째 방법을 제공했습니다. 둘째, 내가 첫 번째 접근법을 제공 한 이유는 최소 코드 변경 (메서드 서명 등)으로 문제를 해결하는 것이 었습니다. 셋째,이 클래스에서 수행되는 모든 작업이 스레드되지 않은 프로그램의 수명 내내 순열을 만드는 경우이 경우 고정 필드를 사용하면 완벽하게 정상적으로 작동합니다. – alfasin

1
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

public class Permute { 

    public static void main(String[] args) { 
     System.out.println(findAllPermutations("abc")); 
    } 

    public static List<String> findAllPermutations(String s) { 
     if (s == null) { 
      throw new NullPointerException(); 
     } 
     if (s.length() <= 1) { 
      return Arrays.asList(s); 
     } 

     List<String> permutations = new ArrayList<>(); 
     for (String permutation : findAllPermutations(s.substring(1))) { 
      char ch = s.charAt(0); 
      for (int i = 0; i <= permutation.length(); i++) { 
       String prefix = permutation.substring(0, i); 
       String suffix = permutation.substring(i); 
       permutations.add(prefix + ch + suffix); 
      } 
     } 
     return permutations; 
    } 
}