Problem with Vector

• 04-01-2010, 08:19 AM
banhbaochay
Problem with Vector
Hi everybody,
I'm a newbie in forum :D. This is my code about testing Vector
Code:

```import java.util.Vector; public class Main { public static void main(String[] args) { Vector v1 = new Vector(); Vector v2 = new Vector(); v2.add("1"); v2.add("2"); v1.add(v2); System.out.println("v2 = " + v2 + ", size = " + v2.size()); System.out.println("v1 = " + v1 + ", size = " + v1.size()); v2.clear(); System.out.println("After clear all elements in v2"); System.out.println("v2 = " + v2 + ", size = " + v2.size()); System.out.println("v1 = " + v1 + ", size = " + v1.size()); } }```
And the output is:
Code:

```v2 = [1,2], size = 2 v1 = [[1,2]], size = 1 After clear all elements in v2 v2 = [], size = 0 v1 = [[]], size = 1```
I don't understand why vector v1 = [[]] and v1's size = 1 after I clear v2?
Can you help me to explain it?
• 04-01-2010, 08:32 AM
gcalvin
You create two Vectors, v1 and v2. You add two Strings to v2, then you add one Vector to v1. So v2's size is 2 and v1's size is 1. Then you clear v2, so its size is 0, but it's still a Vector, and it's still there in v1, so v1's size is still 1.

-Gary-
• 04-01-2010, 08:46 AM
banhbaochay
Thanks gcalvin,
About vector v2 and its size, I can understand. But about vector v1, I don't action on v1, why does v1 change? You explain that: 'cause after clear v2, v2's still a Vector and it's still there in v1, so v1 = [[]].
I have another case:
Code:

```int b = 1; Vector v1 = new Vector(); v1.add(b); System.out.println(v1); b = 2; System.out.println(v1);```
And the output is:
Code:

```[1] [1]```
As you said, the output should be:
Code:

```[1] [2]```
because variable b is still int with value 1 and still there in vector v1?
• 04-01-2010, 09:09 AM
gcalvin
This is the difference between Java primitives (byte, short, int, long, float, double, boolean, char) and Java Objects. Primitives are raw values, but Object variables are references. So when you do this:
Code:

```int b = 1; Vector v1 = new Vector(); v1.add(b);```
v1 stores a 1, not a reference to b. So when you change b, what's in v1 doesn't change. But when you do this:
Code:

```Vector v1 = new Vector(); Vector v2 = new Vector(); v2.add("1"); v2.add("2"); v1.add(v2);```
v1 stores a reference to the Vector v2. v2 and v1.get(0) are now two different names for the same Object. In fact, the variable v2 is itself a reference to the Vector object. All Java Object variables are references.

Hope that helps.

-Gary-
• 04-01-2010, 09:14 AM
banhbaochay
Oh, I understand. Thank you very much gcalvin. :D
• 04-01-2010, 09:42 AM
Tolls
Quote:

Originally Posted by gcalvin
This is the difference between Java primitives (byte, short, int, long, float, double, boolean, char) and Java Objects. Primitives are raw values, but Object variables are references. So when you do this:
Code:

```int b = 1; Vector v1 = new Vector(); v1.add(b);```
v1 stores a 1, not a reference to b. So when you change b, what's in v1 doesn't change.
-Gary-

To be honest, you would get exactly the same result with objects.
eg.
Code:

```Object a = new Object(); Object b = new Object(); Vector v1 = new Vector(); v1.add(b); System.out.println(v1); b = a; System.out.println(v1);```
Both println() will be the same, even though b now references a different object.
• 04-01-2010, 10:29 AM
banhbaochay
I try to test Toll's example. My test is:
Code:

```String b = "abcd"; Vector v1 = new Vector(); v1.add(b); System.out.println(v1); b = "change"; System.out.println(v1);```
And output is:
Code:

```[abcd] [abcd]```
gcalvin, can you explain it for me, please?
• 04-01-2010, 10:35 AM
Tolls
You need to think in terms of references.
b is a reference to an object.
Setting b to a different reference will not change the reference held in the vector.
• 04-01-2010, 04:47 PM
banhbaochay
Thanks Tolls,
I think I need study more about data type of Java :-s
• 04-01-2010, 05:01 PM
gcalvin
Tolls is absolutely correct when he points out that the assignment operator = does not change an object, but simply points the variable to an object.
Code:

```String b = "abcd"; Vector v1 = new Vector(); v1.add(b); System.out.println(v1); b = "change"; System.out.println(v1);```
The string "abcd" did not change to "change", but rather the variable b that once held a reference to "abcd" now holds a reference to "change". Meanwhile, v1.get(0) is still "abcd". However, in the original case:
Code:

```v2.add("1"); v2.add("2"); v1.add(v2); System.out.println("v2 = " + v2 + ", size = " + v2.size()); System.out.println("v1 = " + v1 + ", size = " + v1.size()); v2.clear(); System.out.println("After clear all elements in v2"); System.out.println("v2 = " + v2 + ", size = " + v2.size()); System.out.println("v1 = " + v1 + ", size = " + v1.size());```
v2 is not assigned to point to a different Vector, but the Vector to which v2 (and v1.get(0)) holds a reference has its state changed with the clear() method. Try this for comparison:
Code:

```v2.add("1"); v2.add("2"); v1.add(v2); System.out.println("v2 = " + v2 + ", size = " + v2.size()); System.out.println("v1 = " + v1 + ", size = " + v1.size()); v2 = new Vector(); System.out.println("After assigning a new Vector to v2"); System.out.println("v2 = " + v2 + ", size = " + v2.size()); System.out.println("v1 = " + v1 + ", size = " + v1.size());```
-Gary-