# Thread: Problem with Vector

1. Member
Join Date
Apr 2010
Posts
32
Rep Power
0

## Problem with Vector

Hi everybody,
I'm a newbie in forum :D. This is my code about testing Vector
Java 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:
Java 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?
Thanks for reading..

2. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
7
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-

3. Member
Join Date
Apr 2010
Posts
32
Rep Power
0
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:
Java 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:
Java Code:
```[1]
[1]```
As you said, the output should be:
Java Code:
```[1]
[2]```
because variable b is still int with value 1 and still there in vector v1?

4. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
7
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:
Java 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:
Java 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-

5. Member
Join Date
Apr 2010
Posts
32
Rep Power
0
Oh, I understand. Thank you very much gcalvin. :D

6. Moderator
Join Date
Apr 2009
Posts
13,222
Rep Power
23
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:
Java 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.
Java 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.

7. Member
Join Date
Apr 2010
Posts
32
Rep Power
0
I try to test Toll's example. My test is:
Java Code:
```String b = "abcd";
Vector v1 = new Vector();
v1.add(b);
System.out.println(v1);
b = "change";
System.out.println(v1);```
And output is:
Java Code:
```[abcd]
[abcd]```
gcalvin, can you explain it for me, please?

8. Moderator
Join Date
Apr 2009
Posts
13,222
Rep Power
23
You need to think in terms of references.
b is a reference to an object.
v1.add(b) merely adds that reference to the vector.
Setting b to a different reference will not change the reference held in the vector.

9. Member
Join Date
Apr 2010
Posts
32
Rep Power
0
Thanks Tolls,
I think I need study more about data type of Java :-s

10. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
7
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.
Java 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:
Java 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:
Java 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-

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•