1

we have a following code :

Vector a ; // containing some struct info
Vector b;
b = a ;

if we modify b does it affect on the containing of a ?

Ghassen Bellagha
  • 249
  • 3
  • 5
  • 16
  • Are you actually creating vectors/instances, or are you just declaring them? – Justin L. Sep 10 '13 at 23:54
  • Note that `Vector` is deprecated and has been for a long time. Use a `List` implementation such as `ArrayList`. – Boris the Spider Sep 10 '13 at 23:56
  • @Justin L. i tried it, but it won"t work i posted a more clear question here [link](http://stackoverflow.com/questions/18731950/keeping-original-vector-intact-when-modifying-its-copy) i hope you can take a look – Ghassen Bellagha Sep 11 '13 at 03:40
  • @Boris the Spider i already started the program since 2 months and it's so complicated and i already started it with `Vectors` and now am close to finish it i can't get back to use `list` and `ArrayList` besides i don't know how to use them. if you think you could help me here is a link to the question i tried to make it more clear [Question](http://stackoverflow.com/questions/18731950/keeping-original-vector-intact-when-modifying-its-copy) – Ghassen Bellagha Sep 11 '13 at 03:42

2 Answers2

4

Yes. Both b and a will refer to the same instance of Vector on the heap. This happens with any object, including arrays.

Let's say vector a lies at a hypothetical place called 12345 on the heap. a's value(since A is a reference) is 12345. When b=a is done, b equals 12345 as well. Dereferencing b will land you on the same place on the heap hence the same object.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • ok what is the solution so if i want to copy the vector and modify the second one without affecting the first on ??? – Ghassen Bellagha Sep 10 '13 at 23:55
  • @GhassenBellagha This has been asked countless times. Create a new one, use a copy constructor, or find a vector that implements `Cloneable`. – nanofarad Sep 10 '13 at 23:56
  • You need to create a copy of your Vector, preferably by overriding the clone() method. – Kon Sep 10 '13 at 23:56
  • 1
    @Kon No need, we have [a copy constructor](http://docs.oracle.com/javase/6/docs/api/java/util/Vector.html#Vector(java.util.Collection)). – nanofarad Sep 10 '13 at 23:57
1

Yes! That's a flat copy. To make a deep copy, use Collections:

Vector b = new Vector(a.size()); b.setSize(a.size()); Collections.copy(b,a);

Hope that helps.

edit:

hexafraction is right, the better answer is (using the copy constructor):

Vector b = new Vector(a);
weiglt
  • 1,059
  • 8
  • 15
  • and that way, if we modify `b` it doesn't affect on `a` ? – Ghassen Bellagha Sep 10 '13 at 23:57
  • Yes, exactly! It does not affect it, because it's a new object instance. – weiglt Sep 10 '13 at 23:57
  • This concept is also known as an alias. a is an alias of b, and b is an alias of a – androiddev19 Sep 11 '13 at 03:13
  • @weiglt i tried it, but it won"t work i posted a more clear question here [link](http://stackoverflow.com/questions/18731950/keeping-original-vector-intact-when-modifying-its-copy) i hope you can take a look – Ghassen Bellagha Sep 11 '13 at 03:38
  • @androiddev19 i tried it, but it won"t work i posted a more clear question here [link](http://stackoverflow.com/questions/18731950/keeping-original-vector-intact-when-modifying-its-copy) i hope you can take a look – Ghassen Bellagha Sep 11 '13 at 03:39