How to insert vector element inside vector in java -
i have 2 set of vector , need insert second set in each of first set element , print.
my input : {red,blue,green},{csk,mi,rcb}
my output : {red,csk},{red,mi},{red,rcb},{blue,csk},{blue,mi},{blue,rcb},{green,csk},{green,mi},{green,csk}
i tried code this. can please suggest 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); } }
your expected output can 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
- v3 = v(i).add(v2(j)); line not compile
- no length in vector size
- to element have use get(index) method
- avoid using vector better go arraylist
Comments
Post a Comment