Results 1 to 10 of 10
Thread: Problem with Vector
- 04-01-2010, 08:19 AM #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
And the output is: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()); } }
I don't understand why vector v1 = [[]] and v1's size = 1 after I clear v2?Java Code:v2 = [1,2], size = 2 v1 = [[1,2]], size = 1 After clear all elements in v2 v2 = [], size = 0 v1 = [[]], size = 1
Can you help me to explain it?
Thanks for reading..
- 04-01-2010, 08:32 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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 #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:
And the output is:Java Code:int b = 1; Vector v1 = new Vector(); v1.add(b); System.out.println(v1); b = 2; System.out.println(v1);
As you said, the output should be:Java Code:[1] [1]
because variable b is still int with value 1 and still there in vector v1?Java Code:[1] [2]
- 04-01-2010, 09:09 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
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:int b = 1; Vector v1 = new Vector(); v1.add(b);
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.Java Code:Vector v1 = new Vector(); Vector v2 = new Vector(); v2.add("1"); v2.add("2"); v1.add(v2);
Hope that helps.
-Gary-
- 04-01-2010, 09:14 AM #5
Member
- Join Date
- Apr 2010
- Posts
- 32
- Rep Power
- 0
Oh, I understand. Thank you very much gcalvin. :D
- 04-01-2010, 09:42 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
To be honest, you would get exactly the same result with objects.
eg.
Both println() will be the same, even though b now references a different object.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);
- 04-01-2010, 10:29 AM #7
Member
- Join Date
- Apr 2010
- Posts
- 32
- Rep Power
- 0
I try to test Toll's example. My test is:
And output is:Java Code:String b = "abcd"; Vector v1 = new Vector(); v1.add(b); System.out.println(v1); b = "change"; System.out.println(v1);
gcalvin, can you explain it for me, please?Java Code:[abcd] [abcd]
- 04-01-2010, 10:35 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 04-01-2010, 04:47 PM #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
- 04-01-2010, 05:01 PM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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.
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:String b = "abcd"; Vector v1 = new Vector(); v1.add(b); System.out.println(v1); b = "change"; System.out.println(v1);
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.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());
-Gary-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());
Similar Threads
-
Vector/Table Deleting Row Problem.
By ocean in forum New To JavaReplies: 8Last Post: 12-09-2009, 06:39 PM -
Problem with vector
By morghul in forum New To JavaReplies: 3Last Post: 11-04-2009, 02:24 AM -
Static Vector Problem
By calicocal in forum New To JavaReplies: 12Last Post: 11-09-2008, 11:31 PM -
Vector problem
By Ace_Of_John in forum New To JavaReplies: 1Last Post: 01-27-2008, 08:53 PM -
vector problem
By mambo_jumbo in forum New To JavaReplies: 1Last Post: 11-17-2007, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks