简体   繁体   English

向量内的向量

[英]Vectors within a vector

I have this code...that does just about exactly what I need it to. 我有这段代码...几乎可以满足我的需要。 It searches a predefined array of ints for two ints that sum up to a target int. 它在一个预定义的int数组中搜索两个int,它们总计为一个目标int。 However, when putting values into the vector, rather than placing them within cells, it places all the values together. 但是,将值放入向量中而不是将其放在单元格中时,它将所有值放在一起。 ie for int array[50,40,30,20,10] and target 50, rather than returning [[50][40,10][30,20]...etc.], it prints [[50,40,10,30,20...etc.]] How can I fix this? 即对于int array [50,40,30,20,10]和目标50,而不是返回[[50] [40,10] [30,20] ...等],它打印[[50,40 ,10,30,20 ...等。]我该如何解决?

public Vector<Vector<Integer>> subsetSum(int[] array, int target) {
    //creates vectors, adds inner vector to another vector
    outer = new Vector<Vector<Integer>>();
    inner = new Vector<Integer>();
    outer.add(inner);

    for (int k = 0; k < array.length; k++) {
        for (int l = 1; l < array.length; l++) {
            int sum = array[k]+array[l]; //sum of l and k
            if (sum == target) {
                //add l,k to vector
                inner.add(array[l]);
                inner.add(array[k]);
                //prints l and k if their sum equals target
                System.out.println(array[l]+"+"+array[k]+"="+target);
            }
            else {
                System.out.print("");
            }
        }
        //if k is the target, display
        if (array[k] == target) {
            //add k to vector
            inner.add(array[k]);
            //prints if int equals target
            System.out.println(array[k]+"="+target);
        }
    }
    //return combinations that add up to target in vector form
    return outer;
}

You're only ever adding a single vector to outer . 您只向outer添加了单个向量。 Isn't it the case that if you find a pair that add up to the required sum, you want them in a distinct vector? 如果找到一对加起来等于所需总和的对,不是想要将它们放在不同的向量中吗? So, you need to create a new "inner" vector when that happens, and add it to outer . 因此,您需要在发生这种情况时创建一个新的“内部”向量,并将其添加到outer

Remove these lines: 删除这些行:

inner = new Vector<Integer>();
outer.add(inner);

Change: 更改:

if (sum == target) {
    inner = new Vector<Integer>();
    outer.add(inner)
    //add l,k to vector
    inner.add(array[l]);
    inner.add(array[k]);

And: 和:

if (array[k] == target) {
    inner = new Vector<Integer>();
    outer.add(inner)
    //add k to vector
    inner.add(array[k]);

Finally, consider making inner and outer into local variables. 最后,考虑使innerouter成为局部变量。

I think your mistake lies in add vector value in outer at very first so its return all the value from outer vector only not in the inner vector.. have to added after checking condition 我认为您的错误首先在于在外部添加向量值,因此它仅从外部向量返回所有值,而不是在内部向量中返回..必须在检查条件后添加

    if (array[k] == target) {
        //add k to vector
        inner.add(array[k]);
        //prints if int equals target
        System.out.println(array[k]+"="+target);
    }
    outer.add(inner)

Move these two lines: 移动这两行:

inner = new Vector<Integer>();
outer.add(inner);

into the outer loop (the one with index variable k .) 进入外循环(索引变量为k那个)。

Also, are you intending to have a vector of vectors? 另外,您是否打算拥有向量的向量? because at the moment, you have a vector, with one vector named inner in it, so everything is being added straight into inner. 因为目前您有一个向量,其中有一个名为inner的向量,所以所有内容都直接添加到了inner。 You are not creating a new vector each time to put each pair in. 您并不是每次都在创建新矢量来放入每对。

Deepa's answer is half-way there, but you'll also need to create a new instance of inner inside the for loop before adding the values to it; Deepa的答案就在那儿,但您还需要在for循环inner创建一个inner的新实例,然后再向其中添加值。 otherwise, you'll end up with all the values inside of one inner . 否则,您将最终将所有值包含在一个inner Like this: 像这样:

final Vector<Integer> inner = new Vector<Integer>();
outer.add(inner);

You can also continue after adding to improve performance a bit. 您也可以在添加后continue以提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM