简体   繁体   中英

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. 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?

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 . 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 .

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.

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 .)

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. 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; otherwise, you'll end up with all the values inside of one inner . Like this:

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

You can also continue after adding to improve performance a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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