2013-04-02 35 views
0

我想創建一個雙元組數組。有人會知道這是怎麼完成的?你會如何在Java中創建一個二元數組元組?

我基本上是在尋找有類似以下內容:

int[] values = {5, 1, 7}; // This array length can vary with different values 
int desiredGoal = 77; 

int data[][] = new int[values.length][desiredGoal + 1]; 

每個二維數組的索引將包含一個元組。元組的長度與values數組的長度相同(不管長度是多少),並且包含values數組中值的各種組合以實現所需的目標值。

回答

0

您可以使用java.util.Vector

所以它應該是這樣的:

Vector<Integer> values = new Vector<Integer>(Arrays.asList([5,1,7])); 
Vector<Vector<Integer>> data = new Vector<Vector<Integer>>(values.size()) 
//you'll also need to actually create the inner vector objects: 
for (int i = 0; i<values.size(); i++){ 
    data.set(i,new Vector<Integer>(desiredGoal + 1); 
} 
0

他可能並不需要的Vector同步,所以他應該堅持ListArrayList。假設Java 7,試試這個:

List<Integer> values = new ArrayList<>(); 
Collections.addAll(values, 5, 1, 7); 
int desiredGoal = 77; 
List<List<Integer>> data = new List<>(); 
// Create the inner lists: 
for (final int ignored: values) { 
    data.add(new ArrayList<>(desiredGoal + 1)); 
    // List<Integer> is wanted, so use type inference. 
} 
相關問題