Results 1 to 10 of 10
  1. #1
    banhbaochay is offline Member
    Join Date
    Apr 2010
    Posts
    32
    Rep Power
    0

    Default 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. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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. #3
    banhbaochay is offline Member
    Join Date
    Apr 2010
    Posts
    32
    Rep Power
    0

    Default

    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. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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. #5
    banhbaochay is offline Member
    Join Date
    Apr 2010
    Posts
    32
    Rep Power
    0

    Default

    Oh, I understand. Thank you very much gcalvin. :D

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Quote Originally Posted by gcalvin View Post
    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. #7
    banhbaochay is offline Member
    Join Date
    Apr 2010
    Posts
    32
    Rep Power
    0

    Default

    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. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    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. #9
    banhbaochay is offline Member
    Join Date
    Apr 2010
    Posts
    32
    Rep Power
    0

    Default

    Thanks Tolls,
    I think I need study more about data type of Java :-s

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

Similar Threads

  1. Vector/Table Deleting Row Problem.
    By ocean in forum New To Java
    Replies: 8
    Last Post: 12-09-2009, 06:39 PM
  2. Problem with vector
    By morghul in forum New To Java
    Replies: 3
    Last Post: 11-04-2009, 02:24 AM
  3. Static Vector Problem
    By calicocal in forum New To Java
    Replies: 12
    Last Post: 11-09-2008, 11:31 PM
  4. Vector problem
    By Ace_Of_John in forum New To Java
    Replies: 1
    Last Post: 01-27-2008, 08:53 PM
  5. vector problem
    By mambo_jumbo in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 10:44 PM

Posting Permissions

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