-1

I am trying to pass a vector of vectors from one class to another one. For regular parameters and arrays I used something like this:

public static double[] x= new double[100];

However, this is not working for a vector or a vector of vectors:

public Vector<Vector<Integer>> vec2 = new Vector<Vector<Integer>>();
public Vector<Integer> vec = new Vector<Integer>();

How can we retrieve a vector of vectors in other classes?

Nima
  • 55
  • 8
  • You forgot to make the fields static? Also note: http://stackoverflow.com/questions/1386275 – Durandal Feb 08 '16 at 20:59
  • this is how you define a vector of vectors. vec2 and vec are empty after the `new` calls – DBug Feb 08 '16 at 20:59
  • @Durandal I tried it with static too, it s the same problem – Nima Feb 08 '16 at 21:02
  • Post an [MCVE](http://stackoverflow.com/help/mcve). Be sure to copy-paste your code to a *new project* and make sure it compiles and runs before posting it here. – user1803551 Feb 08 '16 at 21:03
  • @Nima "Not working" is not a sufficient problem description when you give zero context and no indication what you are trying to solve. Describe what problem you have exactly. – Durandal Feb 08 '16 at 21:14

1 Answers1

0

Consider the following code for your actual problem, maybe it could help you understand better :

    //These are the Integers your vector will contain
    Integer i1 = new Integer(1);
    Integer i2 = new Integer(2);
    Integer i3 = new Integer(3);
    Integer i4 = new Integer(4);

    //This is the vector you're going to insert them into (You could create more than one)
    Vector <Integer> v1 = new Vector<>();

    v1.add(i1);
    v1.add(i2);
    v1.add(i3);
    v1.add(i4);

    //This is the vector of vectors you're going to use to contain your previous vector
    Vector <Vector<Integer>> vectorOfVectors= new Vector<>();

    vectorOfVectors.add(v1);

    //To test out your code
    System.out.println(v1.toString());
    System.out.println(vectorOfVectors.toString());

But consider not using the Vector class as suggested in the following post : Why is Java Vector class considered obsolete or deprecated? There are better options such as the Collection Interface's ArrayList.

The Java tutorial offers a complete guide as to how to use the Collections Interface. I suggest you study it closely as it is really useful for any type of collections you could need. Here's the link.

Community
  • 1
  • 1
Imad
  • 2,358
  • 5
  • 26
  • 55