2013-04-02 33 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. 
} 
相关问题