Code Comments
Programming Forum and web based access to our favorite programming groups.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);
}
}
}
Post Follow-up to this message<jrefactors@hotmail.com> wrote in message news:1117737389.740758.239280@z14g2000cwz.googlegroups.com... > // 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".
Post Follow-up to this messageOn Thu, 2 Jun 2005 jrefactors@hotmail.com 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
Post Follow-up to this messageIn article <Pine.GSO.4.58.0506021443410.23508@drj.pf>, . <darrell@does.want.spam.com> wrote: >On Thu, 2 Jun 2005 jrefactors@hotmail.com wrote: > > >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). > >-- >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.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.