1

I have a two set of vector and I need to insert second set in each of the first set element and print.

my input is like : {RED,BLUE,GREEN},{CSK,MI,RCB}

my output is like : {RED,CSK},{RED,MI},{RED,RCB},{BLUE,CSK},{BLUE,MI},{BLUE,RCB},{Green,CSK},{Green,MI},{Green,CSK}

I tried my code like this. Can you please suggest the way

public static void main(String[]args){
Vector v = new Vector();
v.addElement("RED");
v.addElement("BLUE");
v.addElement("GREEN"");
Vector v2 = new Vector();
Vector v3 = new Vector();
v2.addElement("CSK");
v2.addElement("RCB");
v2.addElement("MI");
for(int i=0; i<v.size(); i++){
    for(int j=0; j<v2.length; j++){
     v3 = v(i).add(v2(j));
    }
}
for(int i=0; i<v3.size(); i++){
    System.out.println(v3);
}
}
shanky singh
  • 1,121
  • 2
  • 12
  • 24
  • 2
    Just a side note but it is recommended to use `ArrayList` over `Vector` unless you need a thread-safe implementation. – Thomas Jul 11 '16 at 11:47
  • 2
    this code won't compile. Java doesn't use `var` – Michael Markidis Jul 11 '16 at 11:48
  • That is vector. I correct that. Thanks! – shanky singh Jul 11 '16 at 11:49
  • 1
    Possible duplicate of [Cartesian Products with sets in java](http://stackoverflow.com/questions/21640274/cartesian-products-with-sets-in-java) – niceman Jul 11 '16 at 11:49
  • Have a look at your input, you'll see two lists there. If you look at your output you'll see a list of lists (basically a dynamic 2d array) so try something along those lines (it might be better to use a container object that combines color and whatever the other values represent, but I'll leave that for now). – Thomas Jul 11 '16 at 11:50
  • Btw, `v(i)` doesn't work work as well, I guess you mean `v.get(i)` instead. Additionally have a look at `v.length` and `v++` ... – Thomas Jul 11 '16 at 11:51
  • Can't I use vector here. using two vector and giving their product result in another vector. – shanky singh Jul 11 '16 at 11:52
  • `v3 = v(i).add(v2(j));` its may be simple but I'm not getting this – bananas Jul 11 '16 at 11:52
  • @Thomas `v.length` i can get by `v.size`. – shanky singh Jul 11 '16 at 11:53
  • side note , `v++` is wrong too, `cannot convert from vector to int` – bananas Jul 11 '16 at 11:56
  • `using two vector and giving their product result in another vector` - don't confuse a vector in Java with a vector in maths. The Java class basically is a list and nothing more, there is no vector product. – Thomas Jul 11 '16 at 11:59
  • So I can not achieve this output by using vector. – shanky singh Jul 11 '16 at 12:00
  • No, you _can_ achieve this output by using `Vector`, `List`, an array etc. - just not automagically, you'll have to code it yourself (as I said it's basically a list of lists). – Thomas Jul 11 '16 at 12:07
  • Thanks @Thomas. I choose wrong way of implementation. – shanky singh Jul 11 '16 at 12:09
  • @shankysingh I added answer, please check and read this too (http://www.tutorialspoint.com/java/java_vector_class.htm) – bananas Jul 11 '16 at 12:10
  • Thanks for all your help @asteriskNinja – shanky singh Jul 11 '16 at 12:13
  • you can achieve your goal by using vectors but you need to add vector inside vector so it can hold your pair of values – bananas Jul 11 '16 at 12:16

3 Answers3

3

This is called a cartesian product and works as follows:

List<String> rgbs = Arrays.asList("RED", "GREEN", "BLUE");
List<String> modes = Arrays.asList("CSK", "MI", "RCB");
List<List<String>> cartesianProduct = new ArrayList<>();
for (String rgb : rgbs) {
    for (String mode : modes) {
        List<String> product = new ArrayList<>();
        product.add(rgb);
        product.add(mode);
        cartesianProduct.add(product);
    }
}

I did use a bit of new syntax and classes probably still unknown to you, but quite similar to your code.

The important point is that every element of the result is itself a combination of two strings. Here I used a List.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • One has to begin somewhere, and Vector is there, a venerable grandfather, still called `vector` in C++. List/ArrayList is newer. `List` is an interface, abstract API, and `ArrayList` and implementing class, using an array. Thus one can chose an other implementation like `LinkedList` which would have been better for `product` here: uses less memory. – Joop Eggen Jul 11 '16 at 12:24
3

well I have a solution too..

 public static void main(String[] args) {
            Vector<String> v = new Vector<>();
            v.addElement("RED");
            v.addElement("BLUE");
            v.addElement("GREEN");
            Vector<String> v2 = new Vector<>();
            Vector<String> v3;
            Vector<Vector<String>> v4 = new Vector<>();
            v2.addElement("CSK");
            v2.addElement("RCB");
            v2.addElement("MI");
            for (String vector_v : v) {
                for (String vector_v2 : v2) {
                    v3 = new Vector<>(); // this declares new vector every time and this new vector is
                                        //  added to v4 and thus the list goes
                    v3.add(vector_v2);
                    v3.add(vector_v);
                    v4.add(v3);
                }
            }
            System.out.println(v4);
        }
bananas
  • 1,176
  • 2
  • 19
  • 39
2

your expected output can be achived in below way.

public static void main(String[]args){
        Vector v = new Vector();
        v.addElement("RED");
        v.addElement("BLUE");
        v.addElement("GREEN");
        Vector v2 = new Vector();
        Vector v3 = null;
        v2.addElement("CSK");
        v2.addElement("RCB");
        v2.addElement("MI");
        for(int i=0; i<v.size(); i++){
            for(int j=0; j<v2.size(); j++){
                v3 = new Vector();
                v3.add(v.get(i));
                v3.add(v2.get(j));
                System.out.println(v3);
            }
        }
//      for(int i=0; i<v3.size(); i++){
//          System.out.println(v3);
//      }

from above

  1. v3 = v(i).add(v2(j)); This line will not compile
  2. no length in vector only size
  3. to get the element have to use get(index) method
  4. avoid using vector better go with ArrayList
PrabaharanKathiresan
  • 1,099
  • 2
  • 17
  • 32