2014-01-15 6 views
0

"one two three four"와 같은 문장이 주어지면 순서 순열에서 단어 목록을 어떻게 반복 할 수 있습니까?주어진 문자열의 순서 순열 생성

이 내가 공간 구분 기호를 사용하여 단어를 분할했지만 말은 현재 주문에 보관 모든 조합을 반복하는 방법을 작동하지 않을 수

"one" 
"one two" 
"one two three" 
"one two three four" 
"two" 
"two three" 
"two three four" 
"three" 
"three four" 
"four" 

달성하기 위해 노력하고있어입니다. 사전에

List<String> product_words = new ArrayList<String>(Arrays.asList(productname.split(" "))); 
    for (int x = 0; x < product_words.size(); x++) { 
     //stuck here 
    } 

감사

+1

숙제의 냄새. –

+0

@ NathanielWaggoner 및 ???? – Simon

+0

나는 그것을 의심하므로 52 살입니다! – Mark

답변

1

당신이 이중 for 루프해야하고, (당신이했습니다 후 즉, "하나"를 생략 할 외부 루프의 인덱스에 내부 루프를 시작해야합니다 모든 모든 순열을 거쳤습니다).

ArrayList<String> product_words = new ArrayList<String>(); 
product_words.add("one"); 
product_words.add("two"); 
product_words.add("three"); 
product_words.add("four"); 

for (int i = 0; i < product_words.size(); i++) { 
    String s = ""; 
    for (int j = i; j < product_words.size(); j++) { 
     s += product_words.get(j); 
     s += " "; 
     System.out.println(s); 
    } 
} 
+0

업데이트 해 주셔서 감사합니다. – Mark

+0

@turbo nope, 분명히 작동합니다;) – Tyler