1

I need to set and get a Vector in two different classes but I seem to be losing the size of the Vector in the process. If I do v.size() I get 100 for example. Then I use a setter. Then in another class I use a getter to access this Vector again. If I do v.size() in this new class I get 0. The below code is a rough example of what I have, as I can't copy paste my code exactly, since it's part of a larger private project.

public class Params {
    private Vector<Integer> _v = new Vector<Integer>();
    public Vector<Integer> get_v(){return _v;}
    public void set_v(Vector<Integer> _v){this._v = _v;}
}

public class a {
    v.add(10);
    System.out.println(v.size()); //returns 1
    Params p = new Params();
    p.set_v(v);
}

public class b {
    Params p = new Params();
    v = p.get_v();
    System.out.println(v.size()); //Returns 0
}
  • You have two different instances of `Params`... – Mureinik Jul 25 '17 at 16:40
  • before p.get_v(); you are creating new Params(); = new Vector = 0 size – matoni Jul 25 '17 at 16:40
  • Don't use `Vector`. It's crap and it's more-or-less effectively deprecated. Use a [`List`](https://docs.oracle.com/javase/8/docs/api/java/util/List.html) implementation such as [`ArrayList`](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) or [`LinkedList`](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html) – Michael Jul 25 '17 at 16:41
  • Yeah class a and class b are in two totally different files. I need to reintroduce Params in both files to access the getters and setters inside. – David Payette Jul 25 '17 at 16:41
  • Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector – matoni Jul 25 '17 at 16:42
  • @matoni *Even if* a thread-safe implementation is needed, [it's not recommended to use it](https://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated). – Michael Jul 25 '17 at 16:45

4 Answers4

0

When you make Params = new Params() on b you create a new object of the class Params, and this new object initializes a new empty Vector on the _v property.

You should pass the object created on a to b so this class can use it.

Alan Raso
  • 263
  • 2
  • 9
0

Your problem is you are re initializing your Params class. In essence, you are creating two Params Objects and both have their own Vector, they are completely separate. You could solve this by making your vector static thus making the different instances have the same vector.

Mr. Pasta
  • 35
  • 7
0

You create two difference instances of Params. Each of them has a different variable named _v.

To solve this problem You need to use only a single copy of Params or define _v as static.


Some tips not directly related to the question

Don't use Vector. The use of Vector is deprecated.

If you are not in a multithreading environment use an ArrayList, if you are in a multithreading environment use a synchronized List created with the synchronizedList method of Collections class.

Name your classes in uppercase.

Give readable names to your classes and variables.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • The real problem is that he should be using an IDE that warns him that Vector is deprecated. Otherwise yes he should probably follow what his IDE says... – MCMastery Jul 25 '17 at 16:49
0

Thanks so much for all the help! Wow I've never used this before and I was very pleased with the results. So I solved my problem, I basically just defined my Params once and pass it on to each class instead of recreating it.