2012-02-03 54 views
3

我無法圍繞以下內容進行包裝,例如我有一個List,每個列表都包含'would be'for循環。每個繼承應該在另一個之內。動態'for-loop'結構

所以,如果我有3個對象的列表,我想

class Focus { 
    String focus; 
    List<String> values; 

    public Focus(String focus, String... values) { 
     this.focus = focus; 
     this.values = Lists.newArrayList(values); 
    } 
} 

List<Focus> focuses = new ArrayList<Focus>(); 
focuses.add(new Focus("Focus 1", "09", "14", "13", "12")); 
focuses.add(new Focus("Focus 2", "94", "92")); 
focuses.add(new Focus("Focus 3", "A", "B")); 

String my_string = ""; 
for (Focus obj1 : list_obj_x) { 
    for (Focus obj2 : list_obj_xx) { 
     for (Focus obj3 : list_obj_xxx) { 
      my_string += obj1 + " " + obj2 + " " + obj3; 
     } 
    } 
} 
與列表中的環結構可以成長

明顯,且上面的是不可能的。 我需要一個動態結構來迎合my_string的需求。即:

94 09 A 
94 14 A 
94 13 A 
94 12 A 
94 09 B 
94 14 B 
94 13 B 
94 12 B 
92 09 A 
92 14 A 
92 13 A 
92 12 A 
92 09 B 
92 14 B 
92 13 B 
92 12 B 

輸出應該像上面那樣。 這是我到目前爲止有:

int focusCount = focuses.size(); 
for (int i = (focusCount - 1); i >= 0; i--) { 
    Focus currentFocus = focuses.get(i); 
    List<String> currentFocusValues = currentFocus.values; 

    for (int cfv = 0; cfv < currentFocusValues.size(); cfv++) { 
     String currentFocusValue = currentFocusValues.get(cfv); 

     for (int j = (i - 1); j >= 0; j--) { 
      Focus previousFocus = focuses.get(j); 
      List<String> previousFocusValues = previousFocus.values; 

      for (int pfv = 0; pfv < previousFocusValues.size(); pfv++) { 
       String previousFocusValue = previousFocusValues.get(pfv); 
       System.out.println(currentFocusValue + " " + previousFocusValue); 
      } 
     } 
    } 
} 

它迎合了列表值, 的所有組合,但不是我想要的結構。

有人可以幫我嗎?

+4

IMO你需要寫一個遞歸函數,去一個水平每個「焦點」陣列下來。 – 2012-02-03 13:09:48

+0

或實現一個打印單個焦點對象(或overide toString())的方法,這樣您只能通過焦點對象進行迭代,每個焦點對象都會調用您的eg。用所有參數打印行的print()或toString()方法。 – Kris 2012-02-03 13:16:12

+0

@epoch - 你不能使用StringBuffer而不是String來動態地填充迭代結果嗎?另外,在處理列表等集合時,使用'enhanced-for'循環來獲取集合的值可能會更好。 – 2012-02-03 13:18:15

回答

3

最直接的方法可能是遞歸。在遞歸的每一步中,您都要逐一「鎖定」第n個列表的值,然後逐個列出「列表清單」,直到達到最後。

String[] values = new String[focuses.size()]; 
CreateCombinations(focuses, 0, values); 

使用遞歸方法

private void CreateCombinations(List<Focus> focuses, int index, string[] values) { 
    Focus focus = focuses.get(index); 
    for (string v : focus.values) { 
     values[index] = v; 
     if (index < focuses.size() - 1) { 
      // there is at least one other focus 
      CreateCombinations(focuses, index+1, values); 
     } else { 
      // all values pinned down 
      StringBuilder sb = new StringBuilder(values[0]); 
      for (int i = 1; i < values.length; ++i) { 
       sb.append(" ").append(values[i]); 
      } 
      // now do whatever you like to do with sb.toString()... 
     } 
    } 
} 

當然,這可以進一步細化,但也許這足以作爲一個起點爲您服務。

+0

非常感謝你,我不能相信我錯過了這樣一個簡單的方法 – epoch 2012-02-03 13:36:13

+0

請注意,爲了簡潔起見,我忽略了針對索引超出界限和空引用條件的檢查,您可能希望將它們添加到防止異常。 – 2012-02-03 13:37:31

0

您只有2個循環:1個用於焦點對象,1個用於它們的值。

1

下面是一個迭代的方法(需要清理仍然):

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

public class Focus { 
    String focus; 
    List<String> values; 

    public Focus(String focus, String... values) { 
     this.focus = focus; 
     this.values = Arrays.asList(values); 
    } 

    public static String printAllCombinations(Focus... focuses) { 
     String myString = ""; 
     List<String> allCombinations = new ArrayList<String>(); 

     int length = focuses.length; 

     if (length == 0) { 
      return ""; 
     } else if (length == 1) { 
      allCombinations = focuses[0].values; 
     } else if (length > 1) { 
      for (Focus f : focuses) { 
       allCombinations = getCombinations(allCombinations, f.values); 
      } 
     } 

     for (String s : allCombinations) { 
      myString += s+"\n"; 
     } 

     return myString; 
    } 

    private static List<String> getCombinations(List<String> l1, List<String> l2) { 
     if (l1.size() == 0) {return l2;} 
     else if (l2.size() == 0) {return l1;} 

     List<String> combinations = new ArrayList<String>(); 
     for (String outerValue : l1) { 
      for (String innerValue : l2) { 
       combinations.add(outerValue + " " + innerValue); 
      } 
     } 
     return combinations; 
    } 
} 
0

庫爾特已經提到的,你只需要兩個循環。

你有一個對象列表,焦點對象。每個對象都有一個值列表。因此,你需要一個外環要經過對象的列表,以及另一個循環都要經過值的列表被處理的對象:

mystring = "" 
int m = get the length of focuses list 
for (1 through m) { 
int n = geht the length of values list for the current Focus object 
for (1 through n) { 
    myString += current value; 
} 

}