Vector question

Discussion in 'Java' started by jrefactors@hotmail.com, Jun 2, 2005.

  1. Guest

    In the following program that uses Vector. I don't understand
    what's the differences among 1,2, and 3, because they produce
    the same output. Even I expect (2) and (3) should produce
    buffer overflow. any ideas??


    import java.util.*;

    public class VectorTest
    { public static void main(String[] args)
    {
    Vector v = new Vector(); //(1): Constructs an empty vector so that its
    internal data array has size 10 and its standard capacity increment is
    zero.
    // Vector v = new Vector(3); //(2): Constructs an empty vector with the
    specified initial capacity and with its capacity increment equal to
    zero. I expect to have buffer overflow
    // Vector v = new Vector(3,2); //(3):Constructs an empty vector with
    the specified initial capacity and capacity increment I expect to have
    buffer overflow

    for (int i=0; i < 20; i++)
    {
    v.addElement("Jenny_" + i);
    }

    for (Iterator iter = v.iterator(); iter.hasNext();)
    { String data = (String)iter.next();
    System.out.println(data);
    }
    }
    }
    , Jun 2, 2005
    #1
    1. Advertising

  2. Skip Guest

    <> wrote in message
    news:...

    > // Vector v = new Vector(3); //(2): Constructs an empty vector with the
    > specified initial capacity and with its capacity increment equal to
    > zero. I expect to have buffer overflow


    see that "initial capacity" ? it will grow if you keep adding elements.
    that's why it's called "initial".
    Skip, Jun 2, 2005
    #2
    1. Advertising

  3. . Guest

    On Thu, 2 Jun 2005 wrote:

    > In the following program that uses Vector. I don't understand
    > what's the differences among 1,2, and 3, because they produce
    > the same output. Even I expect (2) and (3) should produce
    > buffer overflow. any ideas??


    A vector is like an array EXCEPT it grows in size as needed. You should
    see no obvious difference between Vector 1, 2 or 3. There might be some
    performance issues. The first Vector is the most efficient for this
    program. The third Vector is the worst.

    > import java.util.*;
    >
    > public class VectorTest
    > { public static void main(String[] args)
    > {
    > Vector v = new Vector(); //(1): Constructs an empty vector so that its
    > internal data array has size 10 and its standard capacity increment is
    > zero.
    > // Vector v = new Vector(3); //(2): Constructs an empty vector with the
    > specified initial capacity and with its capacity increment equal to
    > zero. I expect to have buffer overflow
    > // Vector v = new Vector(3,2); //(3):Constructs an empty vector with
    > the specified initial capacity and capacity increment I expect to have
    > buffer overflow
    >
    > for (int i=0; i < 20; i++)
    > {
    > v.addElement("Jenny_" + i);
    > }
    >
    > for (Iterator iter = v.iterator(); iter.hasNext();)
    > { String data = (String)iter.next();
    > System.out.println(data);
    > }
    > }
    > }


    --
    Send e-mail to: darrell dot grainger at utoronto dot ca
    ., Jun 2, 2005
    #3
  4. Guest

    In article <>,
    .. <> wrote:
    >On Thu, 2 Jun 2005 wrote:
    >
    >> In the following program that uses Vector. I don't understand
    >> what's the differences among 1,2, and 3, because they produce
    >> the same output. Even I expect (2) and (3) should produce
    >> buffer overflow. any ideas??

    >
    >A vector is like an array EXCEPT it grows in size as needed. You should
    >see no obvious difference between Vector 1, 2 or 3. There might be some
    >performance issues. The first Vector is the most efficient for this
    >program. The third Vector is the worst.
    >


    The OP didn't ask and so perhaps has read the documentation for Vector
    more carefully, but:

    The reason for this is that a capacity increment of zero doesn't mean
    "this Vector can't grow" (which is what one might think) but "this
    Vector grows by doubling in size". As you say, that's likely to be
    more efficient.

    And -- anyone else find the use of the term "buffer overflow" not quite
    right in this context? According to the Wikipedia definition (and what
    I usually think), a buffer overflow involves overflowing the intended
    data area and (usually) messing up something else, which is supposed to
    be impossible in Java (one should get some kind of exception instead).

    >> import java.util.*;
    >>
    >> public class VectorTest
    >> { public static void main(String[] args)
    >> {
    >> Vector v = new Vector(); //(1): Constructs an empty vector so that its
    >> internal data array has size 10 and its standard capacity increment is
    >> zero.
    >> // Vector v = new Vector(3); //(2): Constructs an empty vector with the
    >> specified initial capacity and with its capacity increment equal to
    >> zero. I expect to have buffer overflow
    >> // Vector v = new Vector(3,2); //(3):Constructs an empty vector with
    >> the specified initial capacity and capacity increment I expect to have
    >> buffer overflow
    >>
    >> for (int i=0; i < 20; i++)
    >> {
    >> v.addElement("Jenny_" + i);
    >> }
    >>
    >> for (Iterator iter = v.iterator(); iter.hasNext();)
    >> { String data = (String)iter.next();
    >> System.out.println(data);
    >> }
    >> }
    >> }

    >
    >--
    >Send e-mail to: darrell dot grainger at utoronto dot ca
    >



    --
    | B. L. Massingill
    | ObDisclaimer: I don't speak for my employers; they return the favor.
    , Jun 6, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BCC
    Replies:
    12
    Views:
    824
    Clark Cox
    Feb 6, 2004
  2. pmatos
    Replies:
    6
    Views:
    23,724
  3. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    542
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    346
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page